#include <bits/stdc++.h>   using namespace std;   int_fast64_t viz[100001], ans;   vector<int_fast64_t> v(100001);   void dfs(int_fast64_t node) {     ans++;     viz[node] = 1;     if (!viz[v[node]]) dfs(v[node]);   }   void solve() {     int_fast64_t n;     cin >> n;     for (int_fast64_t i = 1; i <= n; i++) cin >> v[i];     vector<int_fast64_t> res;     for (int_fast64_t i = 1; i <= n; i++) {       ans = 0;       if (!viz[i]) dfs(i);       res.push_back(ans);     }     sort(res.begin(), res.end());     if (res.size() > 1) {       int_fast64_t maxi = res[res.size() - 1] + res[res.size() - 2];       res.pop_back();       res.pop_back();       res.push_back(maxi);       ans = 0;       for (int_fast64_t i = 0; i < res.size(); i++) ans += res[i] * res[i];       cout << ans;     } else       cout << n * n;   }   signed main() {     cin.tie(0);     cout.tie(0);     ios_base::sync_with_stdio(0);     ;     solve();     return 0;   }   