#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  return (a / gcd(a, b)) * b;
}
template <class T, class X>
inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}
inline long long getnum() {
  char c = getchar();
  long long num, sign = 1;
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') sign = -1;
  for (num = 0; c >= '0' && c <= '9';) {
    c -= '0';
    num = num * 10 + c;
    c = getchar();
  }
  return num * sign;
}
inline long long power(long long a, long long b) {
  long long multiply = 1;
  for (int i = (0); i < (b); i++) {
    multiply *= a;
  }
  return multiply;
}
int p[100002], cnt, n;
bool visited[100002];
vector<int> graph[100002], take;
void dfs(int u) {
  visited[u] = true;
  cnt++;
  for (auto &v : graph[u]) {
    if (!visited[v]) dfs(v);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int test, cases = 1;
  cin >> n;
  for (int i = (1); i < (n + 1); i++) {
    cin >> p[i];
    graph[i].push_back(p[i]);
    graph[p[i]].push_back(i);
  }
  for (int i = (1); i < (n + 1); i++) {
    if (!visited[i]) {
      cnt = 0;
      dfs(i);
      take.push_back(cnt);
    }
  }
  sort(take.begin(), take.end(), greater<int>());
  if (take.size() == 1) {
    long long ans = (long long)take[0] * take[0];
    cout << ans << "\n";
  } else {
    long long now = take[0] + take[1];
    long long ans = now * now;
    for (int i = 2; i < take.size(); i++) {
      ans += (long long)take[i] * take[i];
    }
    cout << ans << "\n";
  }
  return 0;
}
