#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void ZZ(const char* name, Arg1&& arg1) {
  cerr << name << " = " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void ZZ(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " = " << arg1;
  ZZ(comma, args...);
}
template <typename Arg1>
void ZZZ(Arg1&& arg1) {
  cerr << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void ZZZ(Arg1&& arg1, Args&&... args) {
  cerr << arg1 << ' ';
  ZZZ(args...);
}
const long long N = 1e5 + 5;
vector<long long> adj[N], vis(N, 0);
long long dfs(long long x) {
  vis[x] = 1;
  long long ans = 1;
  for (auto i : adj[x]) {
    if (vis[i]) continue;
    ans += dfs(i);
  }
  return ans;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = (1); i < (n + 1); i++) {
    long long x;
    cin >> x;
    adj[x].push_back(i);
    adj[i].push_back(x);
  }
  vector<long long> aux(2, 0);
  for (long long i = (1); i < (n + 1); i++) {
    if (vis[i]) continue;
    aux.push_back(dfs(i));
  }
  sort(aux.begin(), aux.end());
  long long a = aux.back();
  aux.pop_back();
  long long b = aux.back();
  aux.pop_back();
  aux.push_back(a + b);
  long long ans = 0;
  for (auto i : aux) ans += ((i) * (i));
  cout << ans;
}
