#include <bits/stdc++.h>
using namespace std;
vector<long long int> ve[100099];
bool mark[100099];
long long int ans = 0;
void bfs(int start) {
  queue<int> Q;
  Q.push(start);
  while (!Q.empty()) {
    int top = Q.front();
    Q.pop();
    if (mark[top] == false) continue;
    mark[top] = false;
    ans++;
    for (int i = 0; i < ve[top].size(); i++) {
      if (mark[ve[top][i]] != false) {
        Q.push(ve[top][i]);
      }
    }
  }
}
int main() {
  int n;
  cin >> n;
  vector<long long int> aa;
  long long int ara[n];
  memset(mark, true, sizeof(mark));
  for (int i = 1; i <= n; i++) {
    long long int a;
    cin >> ara[i];
    a = ara[i];
    ve[a].push_back(i);
    ve[i].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (mark[i] == true) {
      bfs(i);
      aa.push_back(ans);
    }
    ans = 0;
  }
  sort(aa.begin(), aa.end());
  long long tt = 0;
  if (aa.size() == 1) {
    cout << aa[0] * aa[0] << endl;
  } else {
    aa[aa.size() - 2] += aa[aa.size() - 1];
    for (int i = 0; i < aa.size() - 1; i++) {
      tt += (long long)(aa[i] * aa[i]);
    }
    cout << tt << endl;
  }
}
