#include <bits/stdc++.h>
using namespace std;
int n;
bool used[100010];
vector<long long> cmp;
vector<vector<int> > g(100010);
void dfs(int v) {
  used[v] = true;
  cmp.back()++;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (!used[to]) {
      dfs(to);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    g[i].push_back(tmp - 1);
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      cmp.push_back(0);
      dfs(i);
    }
  }
  long long ans = 0;
  sort(cmp.rbegin(), cmp.rend());
  if (cmp.size() == 1) {
    cout << cmp[0] * cmp[0];
  } else {
    ans = (cmp[0] + cmp[1]) * (cmp[0] + cmp[1]);
    for (int i = 2; i < cmp.size(); i++) {
      ans += cmp[i] * cmp[i];
    }
    cout << ans;
  }
  return 0;
}
