Statistical mechanics of continual learning: variational principle and mean-field potential