#include <bits/stdc++.h>
using namespace std;
int ss[100009];
long long qq[100009];
int find(int x) {
  if (x != ss[x]) return ss[x] = find(ss[x]);
  return x;
}
int main() {
  int n;
  memset(qq, 0, sizeof(qq));
  cin >> n;
  for (int i = 1; i <= n; i++) ss[i] = i;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    int x = find(i);
    int y = find(k);
    if (x != y) {
      ss[x] = y;
    }
  }
  for (int i = 1; i <= n; i++) {
    qq[find(i)]++;
  }
  sort(qq, qq + n + 1);
  long long ans = 0;
  ans += (qq[n - 1] + qq[n]) * (qq[n - 1] + qq[n]);
  int i = n - 2;
  while (i && qq[i]) {
    ans += qq[i] * qq[i];
    i--;
  }
  cout << ans << endl;
  return 0;
}
