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