#include <bits/stdc++.h>
using namespace std;
long long int Count;
vector<long long int> v[100005];
vector<long long int> com;
bool Mark[100005];
void dfs(int second) {
  Count++;
  Mark[second] = true;
  for (auto w : v[second])
    if (Mark[w] == false) dfs(w);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    long long int x;
    cin >> x;
    v[i].push_back(x);
  }
  for (int i = 1; i <= N; i++) {
    if (Mark[i] == false) {
      Count = 0;
      dfs(i);
      com.push_back(Count);
    }
  }
  sort(com.begin(), com.end());
  long long int sz = com.size();
  if (sz == 1) {
    cout << (long long)N * N << endl;
    return 0;
  }
  long long int ans = 0;
  ans += (com[sz - 1] + com[sz - 2]);
  ans = ((long long)ans * ans);
  for (int i = sz - 3; i >= 0; i--) {
    ans += ((long long)com[i] * com[i]);
  }
  cout << ans << endl;
  return 0;
}
