#include <bits/stdc++.h>
using namespace std;
long long a[100005], p[100005], b[100005];
int n;
long long find(long long x) {
  if (x != a[x]) {
    a[x] = find(a[x]);
  }
  return a[x];
}
int main(void) {
  while (scanf("%lld", &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      a[i] = i;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &p[i]);
      if (find(i) != find(p[i])) {
        a[find(i)] = find(p[i]);
      }
    }
    for (int i = 1; i <= n; i++) {
      b[find(i)]++;
    }
    sort(b + 1, b + n + 1);
    long long ans = 0;
    ans = (b[n] + b[n - 1]) * (b[n] + b[n - 1]);
    for (int i = n - 2; i > 0; i--) {
      if (b[i] == 0) {
        break;
      } else {
        ans += b[i] * b[i];
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
