#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[100001];
long long vis[100001];
long long c;
void dfs(int node) {
  vis[node] = 1;
  c++;
  for (int i = 0; i < adj[node].size(); i++) {
    int child = adj[node][i];
    if (vis[child] == 0) {
      dfs(child);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  long long a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    adj[i].push_back(a[i]);
  }
  vector<long long> v;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      c = 0;
      dfs(i);
      v.push_back(c);
    }
  }
  sort(v.begin(), v.end(), greater<long long>());
  long long ans = 0;
  if (v.size() >= 2) {
    long long p = v[0] + v[1];
    ans = p * p;
    for (int i = 2; i < v.size(); i++) {
      ans += v[i] * v[i];
    }
  } else {
    ans = v[0] * v[0];
  }
  cout << ans << endl;
}
