#include <bits/stdc++.h>
#pragma optimize("Ofast")
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
vector<int> g[N];
bool vis[N];
int cnt;
void dfs(int s) {
  ++cnt;
  vis[s] = true;
  for (auto it : g[s]) {
    if (!vis[it]) {
      dfs(it);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    g[i + 1].push_back(x);
    g[x].push_back(i + 1);
  }
  deque<ll> d;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      cnt = 0;
      dfs(i);
      d.push_back(cnt);
    }
  }
  sort(d.rbegin(), d.rend());
  if (d.size() == 1) d.push_back(0);
  ll ans = (d[0] + d[1]) * (d[0] + d[1]);
  for (int i = 2; i < d.size(); ++i) {
    ans += d[i] * d[i];
  }
  cout << ans << "\n";
  return 0;
}
