#include <bits/stdc++.h>
using namespace std;
long long find(vector<long long>& d, long long a) {
  if (d[a] == -1) {
    return a;
  }
  d[a] = find(d, d[a]);
  return d[a];
}
void join(vector<long long>& d, long long a, long long b) {
  a = find(d, a);
  b = find(d, b);
  if (a == b) {
    return;
  }
  d[a] = b;
}
int main() {
  long long n;
  cin >> n;
  vector<long long> d(n + 1, -1);
  for (long long i = 1; i <= n; i++) {
    long long n;
    cin >> n;
    join(d, i, n);
  }
  unordered_map<long long, long long> m;
  for (long long i = 1; i <= n; i++) {
    m[find(d, i)]++;
  }
  vector<long long> sizes;
  for (auto i : m) {
    sizes.push_back(i.second);
  }
  sort(sizes.begin(), sizes.end());
  if (sizes.size() > 1) {
    long long t = sizes.back();
    sizes.pop_back();
    sizes.back() += t;
  }
  long long total = 0;
  for (auto i : sizes) {
    total += i * i;
  }
  cout << total << endl;
}
