#include <bits/stdc++.h>
using namespace std;
long long c = 0;
int vis[100005];
int a[100005];
void dfs(int i) {
  if (vis[i]) return;
  vis[i] = 1;
  c++;
  dfs(a[i]);
}
int main() {
  int n, i;
  vector<long long> v;
  long long ans = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      c = 0;
      dfs(i);
      v.push_back(c);
    }
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  if (v.size() > 1) {
    ans = (v[0] + v[1]) * (v[0] + v[1]);
    for (i = 2; i < v.size(); i++) {
      ans += v[i] * v[i];
    }
  } else {
    ans = v[0] * v[0];
  }
  cout << ans;
}
