#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long long n, p[N], o[N], niz[N];
int j = 0;
void dfs(int i) {
  o[i] = 1;
  niz[j]++;
  i = p[i];
  if (!o[i]) dfs(i);
}
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &p[i]);
  for (int i = 1; i <= n; i++) o[i] = 0;
  for (int i = 1; i <= n; i++)
    if (!o[i]) {
      niz[++j] = 0;
      dfs(i);
    }
  sort(niz + 1, niz + j + 1);
  if (j == 1)
    printf("%lld ", n * n);
  else if (j == 2)
    printf("%lld ", n * n);
  else {
    long long ans = 0;
    for (int i = 1; i <= j - 3; i++) ans += niz[i] * niz[i];
    long long x = (niz[j] + niz[j - 1]) * (niz[j] + niz[j - 1]);
    long long g = niz[j] + niz[j - 1] + niz[j - 2];
    long long y = ((g * (g - 1)) / 2) - ((niz[j] * (niz[j] - 1)) / 2);
    y += (niz[j] * niz[j]);
    ans += max((x + (niz[j - 2] * niz[j - 2])), y);
    printf("%lld", ans);
  }
  return 0;
}
