#include <bits/stdc++.h>
using namespace std;
long long N;
long long viz[100010];
long long v[100010];
vector<long long> G[100010];
vector<long long> components;
void DFS(long long x) {
  viz[x] = 1;
  components[components.size() - 1]++;
  for (long long i = 0; i < G[x].size(); ++i) {
    if (!viz[G[x][i]]) DFS(G[x][i]);
  }
}
int main() {
  long long N;
  cin >> N;
  long long s = 0;
  for (long long i = 1; i <= N; ++i) {
    cin >> v[i];
    G[i].push_back(v[i]);
  }
  for (long long i = 1; i <= N; ++i) {
    if (viz[i] == 0) {
      components.push_back(0);
      DFS(i);
    }
  }
  sort(components.begin(), components.end());
  long long rez = 0;
  if (components.size() > 1) {
    auto l = components.size();
    long long sum = 0;
    for (long long i = 0; i < l - 2; ++i) sum += components[i] * components[i];
    rez = max(sum + components[l - 2] * components[l - 2] +
                  components[l - 1] * components[l - 1],
              sum + (components[l - 2] + components[l - 1]) *
                        ((components[l - 2] + components[l - 1])));
  } else
    rez = components[0] * components[0];
  cout << rez;
  return 0;
}
