#include <bits/stdc++.h>
using namespace std;
long long int n, k, p[100010], sz = 1, chk[100010];
vector<long long int> G[100010];
priority_queue<long long int> q;
long long int dfs(int x) {
  long long int ret = 1;
  chk[x] = 1;
  for (int y : G[x])
    if (!chk[y]) ret += dfs(y);
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> p[i], G[p[i]].push_back(i), G[i].push_back(p[i]);
  for (int i = 1; i <= n; i++)
    if (!chk[i]) q.push(dfs(i));
  sz = q.top();
  q.pop();
  if (!q.empty()) {
    sz += q.top();
    q.pop();
    sz *= sz;
    while (!q.empty()) sz += (q.top() * q.top()), q.pop();
  } else
    sz *= sz;
  cout << sz;
  return 0;
}
