#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000000 + 9];
int c;
bool vis[100000 + 9];
void dfs(int i) {
  c++;
  vis[i] = true;
  for (int j = 0; j < v[i].size(); j++) {
    if (vis[v[i][j]] != true) {
      dfs(v[i][j]);
    }
  }
}
int main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << "1";
    return 0;
  }
  int x;
  vector<long long int> p;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    v[i].push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] != true) {
      c = 0;
      dfs(i);
      p.push_back(c);
    }
  }
  sort(p.begin(), p.end());
  long long int ans = 0;
  int j = p.size();
  if (j == 1) {
    cout << p[0] * p[0] << endl;
  } else {
    ans += ((p[j - 1] + p[j - 2]) * (p[j - 1] + p[j - 2]));
    for (int i = 0; i < j - 2; i++) {
      ans += (p[i] * p[i]);
    }
    cout << ans;
  }
  return 0;
}
