#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:536870912")
#pragma warning(disable : 4996)
using namespace std;
const int N = 1e5 + 100;
int p[N];
bool used[N];
int dfs(int node) {
  used[node] = 1;
  int r = 1;
  if (!used[p[node]]) r += dfs(p[node]);
  return r;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i]);
    p[i]--;
  }
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      v.push_back(dfs(i));
    }
  }
  sort(v.begin(), v.end());
  if (v.size() > 1) {
    int a = v.back();
    v.pop_back();
    v.back() += a;
  }
  long long int ans = 0;
  for (int i : v) ans += i * (long long int)i;
  printf("%lld", ans);
  return 0;
}
