#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
const long long MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = 4 * atan((long double)1);
long long power(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n % 2) res *= a;
    a *= a;
    n /= 2;
  }
  return res;
}
long long abst(long long a) { return ((a < 0) ? (-1 * a) : (a)); }
long long froot(long long x, long long* root) {
  if (x == root[x])
    return x;
  else
    root[x] = froot(root[x], root);
}
int32_t main() {
  long long n;
  cin >> n;
  long long arr[n + 1], root[n + 1], cnt[n + 1];
  for (long long i = 1; i <= n; i++) {
    cnt[i] = 1;
    root[i] = i;
  }
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
    long long x = froot(i, root);
    long long y = froot(arr[i], root);
    if (x < y) {
      root[y] = x;
      cnt[x] += cnt[y];
    } else if (y < x) {
      root[x] = y;
      cnt[y] += cnt[x];
    }
  }
  map<long long, long long> mp;
  long long max1 = -1, max2 = -1, rm1 = -1, rm2 = -1;
  for (long long i = 1; i <= n; i++) {
    long long x = froot(i, root);
    if (mp[x] == 0) {
      mp[x]++;
      if (cnt[x] >= max1) {
        max2 = max1;
        rm2 = rm1;
        max1 = cnt[x];
        rm1 = x;
      } else if (cnt[x] > max2) {
        max2 = cnt[x];
        rm2 = x;
      }
    }
  }
  if (rm2 == -1) {
    long long res;
    long long x = froot(1, root);
    x = cnt[x];
    res = power(x, 2);
    cout << res << "\n";
  } else {
    long long res = 0;
    root[rm2] = rm1;
    cnt[rm1] += cnt[rm2];
    map<long long, long long> mp1;
    for (long long i = 1; i <= n; i++) {
      long long x = froot(i, root);
      if (mp1[x] == 0) {
        mp1[x]++;
        long long p = cnt[x];
        res += power(p, 2);
      }
    }
    cout << res << "\n";
  }
  return 0;
}
