#include <bits/stdc++.h>
using namespace std;
void c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
vector<long long> adj[100001];
vector<long long> vis(100001, 0);
long long size = 0;
void dfs(long long v) {
  vis[v] = 1;
  size++;
  for (auto u : adj[v])
    if (!vis[u]) dfs(u);
}
int32_t main() {
  c();
  long long n;
  cin >> n;
  long long x;
  for (long long i = (1); i < (n + 1); i++) {
    cin >> x;
    adj[i].push_back(x);
  }
  vector<long long> cc;
  for (long long i = (1); i < (n + 1); i++) {
    if (!vis[i]) {
      dfs(i);
      cc.push_back(size);
      size = 0;
    }
  }
  sort((cc).begin(), (cc).end());
  long long ans = 0;
  long long ind = 0;
  for (auto it = cc.rbegin(); it != cc.rend(); ++it) {
    ind++;
    if (ind == 1)
      ans += *it;
    else if (ind == 2) {
      ans += *it;
      ans *= ans;
    } else
      ans += ((*it) * (*it));
  }
  if (ind == 1) ans *= ans;
  cout << ans;
  return 0;
}
