/*Our biggest fear is not that we are inadequate,
It is that we are powerful beyond measure*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int> adj[100001];
bool visited[100001];
int dfs(int u) {
	visited[u] = true;
	int cnt = 1;
	for(int i = 0 ; i < adj[u].size() ; i ++) {
		if(!visited[adj[u][i]]) {
			cnt ++;
			dfs(adj[u][i]);
		}
	}
	return cnt;
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i ++) {
		int a;	cin >> a;
		adj[i].push_back(a);
	}
	vector<int> graph_lengths;
	for(int i = 1 ; i <= n ; i ++) {
		if(!visited[i]) {
			graph_lengths.push_back(dfs(i));
		}
	}
	sort( graph_lengths.begin(), graph_lengths.end() );
	int len = graph_lengths.size();
	int a = 0;
	if(len > 1) {
		a = graph_lengths[len-1] + graph_lengths[len-2];
	}
	else
		a = graph_lengths[len-1];
	int temp = 0;
	for (int i = 0; i < len-2; i++)
	{
		int m = graph_lengths[i];
		temp += m * (m-1) + m;
	}

	cout << a*(a-1) + a + temp;;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
const int INF = 2e9;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    ll ans = 0;

    vector<int> a(n);
    for(int&x : a) {
		cin >> x;
		x--;
	}

    vector<bool> seen(n, false);
    vector<ll> in;

    for(int i = 0; i < n; i++) {
		if(seen[i]) continue;
		seen[i] = true;
		ll cur = 1;
		int pos = i;
		while(!seen[a[pos]]) {
			pos = a[pos];
			seen[pos] = true;
			cur++;
		}
		in.pb(cur);
	}

	sort(in.begin(), in.end());

	for(int i = 0; i < in.size() - 2; i++) {
		ans += in[i] * in[i];
	}

	ll x = in.back();
	in.pop_back();
	if(in.size()) {
		x += in.back();
	}
	ans += x * x;
	cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
const int INF = 2e9;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    ll ans = 0;

    vector<int> a(n);
    for(int&x : a) {
		cin >> x;
		x--;
	}

    vector<bool> seen(n, false);
    vector<ll> in;

    for(int i = 0; i < n; i++) {
		if(seen[i]) continue;
		seen[i] = true;
		ll cur = 1;
		int pos = i;
		while(!seen[a[pos]]) {
			pos = a[pos];
			seen[pos] = true;
			cur++;
		}
		in.pb(cur);
	}

	sort(in.begin(), in.end());

	for(int i = 0; i < (int) in.size() - 2; i++) {
		ans += in[i] * in[i];
	}

	ll x = in.back();
	in.pop_back();
	if(in.size()) {
		x += in.back();
	}
	ans += x * x;
	cout << ans << '\n';

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long int n;
    cin>>n;
    long int p[n+1];
    for(long int i=1;i<=n;i++)
    {
        cin>>p[i];
    }
    long int visited[n+1]={0};
    long long int groups[n]={0};
    long int curr_grp=0;
    for(long int i=1;i<=n;i++)
    {
        long int pos = i;
        if(visited[i])
            continue;

        while(!visited[pos])
        {
            //cout<<pos<<endl;
            groups[curr_grp]++;
            visited[pos]=1;
            pos = p[pos];
        }
        curr_grp++;
    }
    sort(groups,groups+curr_grp,greater<>());
    // if(n==99997 && p[1]==39210)
    // {
    //     for(int i=0;i<curr_grp;i++)
    //     {
    //         cout<<groups[i]<<" ";
    //     }
    //     cout<<endl;
    //     //cout<<"curr_grp : "<<curr_grp<<endl;
    // }
    if(curr_grp==1)
        cout<<(groups[0]*groups[0])<<endl;
    else
    {
        long long int ans = (groups[0]+groups[1])*(groups[0]+groups[1]);
        for(long int i=2;i<curr_grp;i++)
        {
            ans += groups[i]*groups[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int p[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>p[i];
    }
    int visited[n+1]={0};
    int groups[n]={0};
    int curr_grp=0;
    for(int i=1;i<=n;i++)
    {
        int pos = i;
        if(visited[i])
            continue;

        while(!visited[pos])
        {
            //cout<<pos<<endl;
            groups[curr_grp]++;
            visited[pos]=1;
            pos = p[pos];
        }
        curr_grp++;
    }
    sort(groups,groups+curr_grp,greater<>());
    if(curr_grp==1)
    cout<<(groups[0]*groups[0])<<endl;
    else
    {
        long int ans = (groups[0]+groups[1])*(groups[0]+groups[1]);
        for(int i=2;i<curr_grp;i++)
        {
            ans += groups[i]*groups[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long int n;
    cin>>n;
    long int p[n+1];
    for(long int i=1;i<=n;i++)
    {
        cin>>p[i];
    }
    long int visited[n+1]={0};
    long int groups[n]={0};
    long int curr_grp=0;
    for(long int i=1;i<=n;i++)
    {
        long int pos = i;
        if(visited[i])
            continue;

        while(!visited[pos])
        {
            //cout<<pos<<endl;
            groups[curr_grp]++;
            visited[pos]=1;
            pos = p[pos];
        }
        curr_grp++;
    }
    sort(groups,groups+curr_grp,greater<>());
    if(n==99997 && p[1]==39210)
    {
        cout<<"curr_grp : "<<curr_grp<<endl;
    }
    if(curr_grp==1)
        cout<<(groups[0]*groups[0])<<endl;
    else
    {
        long long int ans = (groups[0]+groups[1])*(groups[0]+groups[1]);
        for(long int i=2;i<curr_grp;i++)
        {
            ans += groups[i]*groups[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long int n;
    cin>>n;
    long int p[n+1];
    for(long int i=1;i<=n;i++)
    {
        cin>>p[i];
    }
    long int visited[n+1]={0};
    long int groups[n]={0};
    long int curr_grp=0;
    for(long int i=1;i<=n;i++)
    {
        long int pos = i;
        if(visited[i])
            continue;

        while(!visited[pos])
        {
            //cout<<pos<<endl;
            groups[curr_grp]++;
            visited[pos]=1;
            pos = p[pos];
        }
        curr_grp++;
    }
    sort(groups,groups+curr_grp,greater<>());
    if(curr_grp==1)
    cout<<(groups[0]*groups[0])<<endl;
    else
    {
        long long int ans = (groups[0]+groups[1])*(groups[0]+groups[1]);
        for(long int i=2;i<curr_grp;i++)
        {
            ans += groups[i]*groups[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long int n;
    cin>>n;
    long int p[n+1];
    for(long int i=1;i<=n;i++)
    {
        cin>>p[i];
    }
    long int visited[n+1]={0};
    long int groups[n]={0};
    long int curr_grp=0;
    for(long int i=1;i<=n;i++)
    {
        long int pos = i;
        if(visited[i])
            continue;

        while(!visited[pos])
        {
            //cout<<pos<<endl;
            groups[curr_grp]++;
            visited[pos]=1;
            pos = p[pos];
        }
        curr_grp++;
    }
    sort(groups,groups+curr_grp,greater<>());
    if(n==99997 && p[1]==39210)
    {
        for(int i=0;i<curr_grp;i++)
        {
            cout<<groups[i]<<" ";
        }
        cout<<endl;
        //cout<<"curr_grp : "<<curr_grp<<endl;
    }
    if(curr_grp==1)
        cout<<(groups[0]*groups[0])<<endl;
    else
    {
        long long int ans = (groups[0]+groups[1])*(groups[0]+groups[1]);
        for(long int i=2;i<curr_grp;i++)
        {
            ans += groups[i]*groups[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long int n;
    cin>>n;
    long int p[n+1];
    for(long int i=1;i<=n;i++)
    {
        cin>>p[i];
    }
    long int visited[n+1]={0};
    long int groups[n]={0};
    long int curr_grp=0;
    for(long int i=1;i<=n;i++)
    {
        long int pos = i;
        if(visited[i])
            continue;

        while(!visited[pos])
        {
            //cout<<pos<<endl;
            groups[curr_grp]++;
            visited[pos]=1;
            pos = p[pos];
        }
        curr_grp++;
    }
    sort(groups,groups+curr_grp,greater<>());
    if(curr_grp==1)
        cout<<(groups[0]*groups[0])<<endl;
    else if(curr_grp>=3 && groups[1]==1)
    {
        long long int ans = (groups[0]+2)*(groups[0]+2);
        for(long int i=3;i<curr_grp;i++)
        ans+= groups[i]*groups[i];
        cout<<ans<<endl;
    }
    else
    {
        long long int ans = (groups[0]+groups[1])*(groups[0]+groups[1]);
        for(long int i=2;i<curr_grp;i++)
        {
            ans += groups[i]*groups[i];
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653
#define N 100005

using namespace std;

int dx[9]={1, -1, 0, 0, 1, 1, -1, -1, 0};
int dy[9]={0, 0, 1, -1, 1, -1, 1, -1, 0};

bool vis[N];
int cnt;

void dfs (vector<ll> g[], int t)
{
        vis[t]=true;
        cnt++;
        for(int i=0; i<g[t].size(); i++)
        {
                if(vis[g[t][i]]==false) dfs(g, g[t][i]);
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll n, i, x, ans=0;
        cin>>n;
        vector<ll> g[N], cc;
        for(i=1; i<=n; i++)
        {
                cin>>x;
                g[x].pb(i);
                vis[i]=false;
        }
        for(i=1; i<=n; i++)
        {
                cnt=0;
                if(vis[i]==false){
                        dfs(g, i);
                        cc.pb(cnt);
                }
        }
        if(cc.size()==1) cout<<n*n;
        else{
                sort(cc.begin(), cc.end());
                ans += (cc[cc.size()-1]+cc[cc.size()-2])*(cc[cc.size()-1]+cc[cc.size()-2]);
                cc.pop_back(); cc.pop_back();
                for(i=0; i<cc.size(); i++) ans+= (cc[i]*cc[i]);
                cout<<ans;
        }
        return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
#define pi 3.141592653
#define N 100005

using namespace std;

int dx[9]={1, -1, 0, 0, 1, 1, -1, -1, 0};
int dy[9]={0, 0, 1, -1, 1, -1, 1, -1, 0};

bool vis[N];
int cnt;

void dfs (vector<ll> g[], int t)
{
        vis[t]=true;
        cnt++;
        for(int i=0; i<g[t].size(); i++)
        {
                if(vis[g[t][i]]==false) dfs(g, g[t][i]);
        }
}

int main()
{
        ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
        ll n, i, x, ans=0;
        cin>>n;
        vector<ll> g[N], cc;
        for(i=1; i<=n; i++)
        {
                cin>>x;
                g[x].pb(i);
                vis[i]=false;
        }
        for(i=1; i<=n; i++)
        {
                cnt=0;
                if(vis[i]==false){
                        dfs(g, i);
                        cc.pb(cnt);
                }
        }
        if(cc.size()==1) cout<<n*n;
        else{
                sort(cc.begin(), cc.end());
                ans += (cc[cc.size()-1]+cc[cc.size()-2])*(cc[cc.size()-1]+cc[cc.size()-2]);
                cc.pop_back(); cc.pop_back();
                for(i=0; i<cc.size(); i++) ans+=cc[i]*cc[i];
        }
        cout<<ans;
        return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
constexpr int mod = 1e9 + 7;
constexpr int inf = (1 << 30) - 1;
constexpr ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr)
#define set_digit(N) cout << fixed << setprecision((N))
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// 0-indexed
struct UnionFind
{
	vector<int> data, pointer;
	int component;

	UnionFind(int sz)
	{
		data.assign(sz, -1);
		pointer.assign(sz, 0);

		for (int i = 0; i < sz; i++)
		{
			pointer[i] = i;
		}

		component = sz;
	}

	bool unite(int x, int y)
	{
		x = root(x), y = root(y);
		if (x == y)
			return false;
		if (data[x] > data[y])
			swap(x, y);
		swap(pointer[x], pointer[y]);
		component--;
		data[x] += data[y];
		data[y] = x;
		return true;
	}

	int root(int k)
	{
		if (data[k] < 0)
			return k;
		data[k] = root(data[k]);
		return data[k];
	}

	int size(int k)
	{
		return (-data[root(k)]);
	}

	bool same(int x, int y)
	{
		return (root(x) == root(y));
	}

	int connected_component()
	{
		return component;
	}

	vector<int> get_set(int x)
	{
		vector<int> res;
		int y = x;
		res.push_back(x);
		while (pointer[y] != x)
		{
			y = pointer[y];
			res.push_back(y);
		}

		return res;
	}
};

int main()
{
	fast();

	ll N;
	cin >> N;
	UnionFind uf(N);
	for (int i = 0; i < N; i++)
	{
		int a;
		cin >> a;
		a--;
		uf.unite(a, i);
	}

	vector<int> visited(N);
	vector<ll> ufsz;
	for (int i = 0; i < N; i++)
	{
		if (visited[i])
			continue;
		auto st = uf.get_set(i);

		for (int j = 0; j < st.size(); j++)
		{
			visited[st[j]] = true;
		}
		assert((int)st.size() == (int)uf.size(i));
		ufsz.push_back((ll)st.size());
	}

	sort(ufsz.rbegin(), ufsz.rend());

	if (ufsz.size() == 1)
	{
		cout << N * N << "\n";
		return 0;
	}
	ll a = ufsz[0] + ufsz[1], ans = a * a;

	for (int i = 2; i < ufsz.size(); i++)
	{
		a = ufsz[i];
		ans += a * a;
	}

	cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
constexpr int mod = 1e9 + 7;
constexpr int inf = (1 << 30) - 1;
constexpr ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr)
#define set_digit(N) cout << fixed << setprecision((N))
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// 0-indexed
struct UnionFind
{
	vector<int> data, pointer;
	int component;

	UnionFind(int sz)
	{
		data.assign(sz, -1);
		pointer.assign(sz, 0);

		for (int i = 0; i < sz; i++)
		{
			pointer[i] = i;
		}

		component = sz;
	}

	bool unite(int x, int y)
	{
		x = root(x), y = root(y);
		if (x == y)
			return false;
		if (data[x] > data[y])
			swap(x, y);
		swap(pointer[x], pointer[y]);
		component--;
		data[x] += data[y];
		data[y] = x;
		return true;
	}

	int root(int k)
	{
		if (data[k] < 0)
			return k;
		data[k] = root(data[k]);
		return data[k];
	}

	int size(int k)
	{
		return (-data[root(k)]);
	}

	bool same(int x, int y)
	{
		return (root(x) == root(y));
	}

	int connected_component()
	{
		return component;
	}

	vector<int> get_set(int x)
	{
		vector<int> res;
		int y = x;
		res.push_back(x);
		while (pointer[y] != x)
		{
			y = pointer[y];
			res.push_back(y);
		}

		return res;
	}
};

int main()
{
	fast();

	ll N;
	cin >> N;
	UnionFind uf(N);
	for (int i = 0; i < N; i++)
	{
		int a;
		cin >> a;
		a--;
		uf.unite(a, i);
	}

	vector<int> visited(N);
	vector<ll> ufsz;
	for (int i = 0; i < N; i++)
	{
		if (visited[i])
			continue;
		auto st = uf.get_set(i);

		for (int j = 0; j < st.size(); j++)
		{
			visited[st[j]] = true;
		}

		ufsz.push_back((ll)st.size());
	}

	sort(ufsz.rbegin(), ufsz.rend());

	if (ufsz.size() == 1)
	{
		cout << N * (N - 1) / 2 << "\n";
		return 0;
	}

	ll a = ufsz[0] + ufsz[1], ans = a * a;

	for (int i = 2; i < ufsz.size(); i++)
	{
		a = ufsz[i];
		ans += a;
	}

	cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using ll = int_fast64_t;
constexpr int mod = 1e9 + 7;
constexpr int inf = (1 << 30) - 1;
constexpr ll infll = (1LL << 61) - 1;
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr)
#define set_digit(N) cout << fixed << setprecision((N))
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

// 0-indexed
struct UnionFind
{
	vector<int> data, pointer;
	int component;

	UnionFind(int sz)
	{
		data.assign(sz, -1);
		pointer.assign(sz, 0);

		for (int i = 0; i < sz; i++)
		{
			pointer[i] = i;
		}

		component = sz;
	}

	bool unite(int x, int y)
	{
		x = root(x), y = root(y);
		if (x == y)
			return false;
		if (data[x] > data[y])
			swap(x, y);
		swap(pointer[x], pointer[y]);
		component--;
		data[x] += data[y];
		data[y] = x;
		return true;
	}

	int root(int k)
	{
		if (data[k] < 0)
			return k;
		data[k] = root(data[k]);
		return data[k];
	}

	int size(int k)
	{
		return (-data[root(k)]);
	}

	bool same(int x, int y)
	{
		return (root(x) == root(y));
	}

	int connected_component()
	{
		return component;
	}

	vector<int> get_set(int x)
	{
		vector<int> res;
		int y = x;
		res.push_back(x);
		while (pointer[y] != x)
		{
			y = pointer[y];
			res.push_back(y);
		}

		return res;
	}
};

int main()
{
	fast();

	ll N;
	cin >> N;
	UnionFind uf(N);
	for (int i = 0; i < N; i++)
	{
		int a;
		cin >> a;
		a--;
		uf.unite(a, i);
	}

	vector<int> visited(N);
	vector<ll> ufsz;
	for (int i = 0; i < N; i++)
	{
		if (visited[i])
			continue;
		auto st = uf.get_set(i);

		for (int j = 0; j < st.size(); j++)
		{
			visited[st[j]] = true;
		}

		ufsz.push_back((ll)st.size());
	}

	sort(ufsz.rbegin(), ufsz.rend());

	if (ufsz.size() == 1)
	{
		cout << N * N << "\n";
		return 0;
	}

	ll a = ufsz[0] + ufsz[1], ans = a * a;

	for (int i = 2; i < ufsz.size(); i++)
	{
		a = ufsz[i];
		ans += a;
	}

	cout << ans << "\n";
}


/*
	Author: [UFC-QXD] Daniel Vitor Pereira Rodrigues <danielvitor@alu.ufc.br>
	Problem:
	Link:
	Origin:
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long i64;
typedef pair<int, int> ii;
typedef pair<i64, i64> ll;
typedef vector<int> vi;
typedef vector<i64> vi64;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

const double eps = 1e-9;

#define eq(a, b) (abs(a - b) < eps)
#define lt(a, b) ((a + eps) < b)
#define gt(a, b) (a > (b + eps))
#define le(a, b) (a < (b + eps))
#define ge(a, b) ((a + eps) > b)

#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ms(a, x) memset(a, x, sizeof(a))
#define len(x) (x).size()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

const int dtx[] = { 0, 0, -1, 1, 1, -1,  1, -1};
const int dty[] = {-1, 1,  0, 0, 1, -1, -1,  1};
const int dtxc[] = {1,  1, 2,  2, -1, -1, -2, -2};
const int dtyc[] = {2, -2, 1, -1,  2, -2,  1, -1};

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+5;
const int mod = 1e9+7;

int n, p;
vi gr[maxn];
int sum;

bool used[maxn];

void dfs(int u) {
	++sum;
	used[u] = true;
	for (int to : gr[u]) {
		if (!used[to]) {
			dfs(to);
		}
	}
}

int main() {
	fastIO();

	cin >> n;

	for (int i = 1; i <= n; ++i) {
		cin >> p;
		gr[i].eb(p);
	}

	priority_queue<int> pq;

	for (int i = 1; i <= n; ++i) {
		if (!used[i]) {
			sum = 0;
			dfs(i);
			pq.push(sum);
		}
	}

	if (pq.size() == 1) {
		i64 ans = pq.top();
		ans = ans * ans;
		cout << ans << '\n';
	} else {
		i64 ans = pq.top(); pq.pop();
		ans += pq.top(); pq.pop();
		ans = ans * ans;
		while (!pq.empty()) {
			ans += pq.top() * 1LL * pq.top();
			pq.pop();
		}
		cout << ans << '\n';
	}

	return 0;
}


/*
	Author: [UFC-QXD] Daniel Vitor Pereira Rodrigues <danielvitor@alu.ufc.br>
	Problem:
	Link:
	Origin:
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long i64;
typedef pair<int, int> ii;
typedef pair<i64, i64> ll;
typedef vector<int> vi;
typedef vector<i64> vi64;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

const double eps = 1e-9;

#define eq(a, b) (abs(a - b) < eps)
#define lt(a, b) ((a + eps) < b)
#define gt(a, b) (a > (b + eps))
#define le(a, b) (a < (b + eps))
#define ge(a, b) ((a + eps) > b)

#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ms(a, x) memset(a, x, sizeof(a))
#define len(x) (x).size()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

const int dtx[] = { 0, 0, -1, 1, 1, -1,  1, -1};
const int dty[] = {-1, 1,  0, 0, 1, -1, -1,  1};
const int dtxc[] = {1,  1, 2,  2, -1, -1, -2, -2};
const int dtyc[] = {2, -2, 1, -1,  2, -2,  1, -1};

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+5;
const int mod = 1e9+7;

int n, p;
vi gr[maxn];
int sum;

bool used[maxn];

void dfs(int u) {
	++sum;
	used[u] = true;
	for (int to : gr[u]) {
		if (!used[to]) {
			dfs(to);
		}
	}
}

int main() {
	fastIO();

	cin >> n;

	for (int i = 1; i <= n; ++i) {
		cin >> p;
		gr[i].eb(p);
	}

	priority_queue<int> pq;

	for (int i = 1; i <= n; ++i) {
		if (!used[i]) {
			sum = 0;
			dfs(i);
			pq.push(sum);
		}
	}

	if (pq.size() == 1) {
		i64 ans = pq.size();
		ans = ans * ans;
		cout << ans << '\n';
	} else {
		i64 ans = pq.top(); pq.pop();
		ans += pq.top(); pq.pop();
		ans = ans * ans;
		while (!pq.empty()) {
			ans += pq.top() * 1LL * pq.top();
			pq.pop();
		}
		cout << ans << '\n';
	}

	return 0;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;

void solve(){
    int n; sci(n);
    vi A(n); for(int &x : A) sci(x);
    vi loop;
    vi vis(n,0);
    LL ret = 0;
    for(int i = 0; i < n; i++){
        if(vis[i]) continue;
        int u = i, sz = 0;
        while(!vis[u]){
            sz++;
            vis[u] = true;
            u = A[u] - 1;
        }
        loop << sz;
        ret += 1ll * sz * sz;
    }
    sort(all(loop),greater<int>());
    if(loop.size()==1) cout << ret << endl;
    else ret += (loop[0]+loop[1]+0ll) * (loop[0]+loop[1]) - 1ll * loop[0] * loop[0] - 1ll * loop[1] * loop[1], cout << ret << endl;

}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Local.in","r",stdin);
    freopen("ans.out","w",stdout);
    #endif
    solve();
    return 0;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x)  cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;

void solve(){
    int n; sci(n);
    vi A(n); for(int &x : A) sci(x);
    vi loop;
    vi vis(n,0);
    LL ret = 0;
    for(int i = 0; i < n; i++){
        if(vis[i]) continue;
        int u = i, sz = 0;
        while(!vis[u]){
            sz++;
            vis[u] = true;
            u = A[u] - 1;
        }
        loop << sz;
        ret += sz * sz;
    }
    sort(all(loop),greater<int>());
    if(loop.size()==1) cout << ret << endl;
    else ret += (loop[0]+loop[1]+0ll) * (loop[0]+loop[1]) - 1ll * loop[0] * loop[0] - 1ll * loop[1] * loop[1], cout << ret << endl;

}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("Local.in","r",stdin);
    freopen("ans.out","w",stdout);
    #endif
    solve();
    return 0;
}


/* Author : tushar3105 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef pair<long long int,long long int> ii;					// Pair of integers
typedef vector<long long int> vi;                   				// Vector of integers
typedef vector<vector<long long int>> vvi;                 			// Vector of vi
typedef vector<pair<long long int, long long int>> vii;				// Vector of ii

#define pb push_back								// Push back to vectors
#define mp make_pair								// For pairs
#define ff first								// For pairs
#define ss second								// For pairs
#define all(c) (c).begin(),(c).end()						// Use in sorting
#define sz(a) ((long long int)(a).size())
#define lli long long int
#define ull unsigned long long int
#define ld long double
#define ref(i,x,y) for(long long int i=(x);i<=(y);++i)				// Loop to go forward
#define reb(i,x,y) for(long long int i=(x);i>=(y);--i)				// Loop to go backward
#define trf(c,it) for(auto it = (c).begin(); it != (c).end(); it++)		// Remember it is an iterator
#define trb(c,it) for(auto it = (c).end()-1; it != (c).begin()-1; it--)		// Remember it is an iterator
#define tc(t) long long int t; cin>>t;while(t--)				// Test cases
//fill (dist,dist+110000,pinf);							// For setting default

const long long int mod = 1e9+7;
const long long int pinf = 9223372036854775807;
const long long int ninf = -9223372036854775807;

/*-----------------------------Code begins----------------------------------*/

lli n, x, vis[110000], arr[110000], top=0;
vi adj[110000];

lli dfs (lli x1)
{
	stack<lli> s;
	vis[x1]=1;
	s.push(x1);
	lli sum=0;

	while (!s.empty())
	{
		sum++;
		x1=s.top();
		s.pop();

		trf(adj[x1],it)
		{
			if (vis[*it]==0)
			{
				vis[*it]=1;
				s.push(*it);
			}
		}
	}

	return sum;
}

int main (void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	lli sum=0;

	cin>>n;

	ref(i,0,n-1)
	{
		cin>>x;
		adj[i].pb(x);
	}

	if (n==1)
	{
		cout<<1<<endl;
		return 0;
	}

	ref(i,1,n)
		if (vis[i]==0)
			arr[top++]=dfs(i);

	sort(arr, arr+top);

	ref(i,0,top-3)
		sum=sum+arr[i]*arr[i];

	sum=sum+(arr[top-2]+arr[top-1])*(arr[top-2]+arr[top-1]);

	cout<<sum<<endl;

	return 0;
}


/* Author : tushar3105 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef pair<long long int,long long int> ii;					// Pair of integers
typedef vector<long long int> vi;                   				// Vector of integers
typedef vector<vector<long long int>> vvi;                 			// Vector of vi
typedef vector<pair<long long int, long long int>> vii;				// Vector of ii

#define pb push_back								// Push back to vectors
#define mp make_pair								// For pairs
#define ff first								// For pairs
#define ss second								// For pairs
#define all(c) (c).begin(),(c).end()						// Use in sorting
#define sz(a) ((long long int)(a).size())
#define lli long long int
#define ull unsigned long long int
#define ld long double
#define ref(i,x,y) for(long long int i=(x);i<=(y);++i)				// Loop to go forward
#define reb(i,x,y) for(long long int i=(x);i>=(y);--i)				// Loop to go backward
#define trf(c,it) for(auto it = (c).begin(); it != (c).end(); it++)		// Remember it is an iterator
#define trb(c,it) for(auto it = (c).end()-1; it != (c).begin()-1; it--)		// Remember it is an iterator
#define tc(t) long long int t; cin>>t;while(t--)				// Test cases
//fill (dist,dist+110000,pinf);							// For setting default

const long long int mod = 1e9+7;
const long long int pinf = 9223372036854775807;
const long long int ninf = -9223372036854775807;

/*-----------------------------Code begins----------------------------------*/

lli n, x, vis[110000], arr[110000], top=0;
vi adj[110000];

lli dfs (lli x1)
{
	stack<lli> s;
	vis[x1]=1;
	s.push(x1);
	lli sum=0;

	while (!s.empty())
	{
		sum++;
		x1=s.top();
		s.pop();

		trf(adj[x1],it)
		{
			if (vis[*it]==0)
			{
				vis[*it]=1;
				s.push(*it);
			}
		}
	}

	return sum;
}

int main (void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	lli sum=0;

	cin>>n;

	ref(i,1,n)
	{
		cin>>x;
		adj[i].pb(x);
	}

	if (n==1)
	{
		cout<<1<<endl;
		return 0;
	}

	ref(i,1,n)
		if (vis[i]==0)
			arr[top++]=dfs(i);

	sort(arr, arr+top);

	ref(i,0,top-3)
		sum=sum+arr[i]*arr[i];

	sum=sum+(arr[top-2]+arr[top-1])*(arr[top-2]+arr[top-1]);

	cout<<sum<<endl;

	return 0;
}


/* Author : tushar3105 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef pair<long long int,long long int> ii;					// Pair of integers
typedef vector<long long int> vi;                   				// Vector of integers
typedef vector<vector<long long int>> vvi;                 			// Vector of vi
typedef vector<pair<long long int, long long int>> vii;				// Vector of ii

#define pb push_back								// Push back to vectors
#define mp make_pair								// For pairs
#define ff first								// For pairs
#define ss second								// For pairs
#define all(c) (c).begin(),(c).end()						// Use in sorting
#define sz(a) ((long long int)(a).size())
#define lli long long int
#define ull unsigned long long int
#define ld long double
#define ref(i,x,y) for(long long int i=(x);i<=(y);++i)				// Loop to go forward
#define reb(i,x,y) for(long long int i=(x);i>=(y);--i)				// Loop to go backward
#define trf(c,it) for(auto it = (c).begin(); it != (c).end(); it++)		// Remember it is an iterator
#define trb(c,it) for(auto it = (c).end()-1; it != (c).begin()-1; it--)		// Remember it is an iterator
#define tc(t) long long int t; cin>>t;while(t--)				// Test cases
//fill (dist,dist+110000,pinf);							// For setting default

const long long int mod = 1e9+7;
const long long int pinf = 9223372036854775807;
const long long int ninf = -9223372036854775807;

/*-----------------------------Code begins----------------------------------*/

lli n, x, vis[110000], arr[110000], top=0;
vi adj[110000];

lli dfs (lli x1)
{
	stack<lli> s;
	vis[x1]=1;
	s.push(x1);
	lli sum=0;

	while (!s.empty())
	{
		sum++;
		x1=s.top();
		s.pop();

		trf(adj[x1],it)
		{
			if (vis[*it]==0)
			{
				vis[*it]=1;
				s.push(*it);
			}
		}
	}

	return sum;
}

int main (void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	lli sum=0;

	cin>>n;

	ref(i,0,n-1)
	{
		cin>>x;
		adj[i].pb(x);
	}

	ref(i,1,n)
		if (vis[i]==0)
			arr[top++]=dfs(i);

	sort(arr, arr+top);

	ref(i,0,top-3)
		sum=sum+arr[i]*arr[i];

	sum=sum+(arr[top-2]+arr[top-1])*(arr[top-2]+arr[top-1]);

	cout<<sum<<endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

//c++ templte for help.......................................................................................................................................................

typedef long long ll;

template<typename... T>
void print1(T... t){
	((cout<<t<<" "),...);
}

template<typename... T>
void print2(T... t){
		vector<vector<ll>> ans={t...};
		for(ll i=0;i<(ll)ans.size();i++){
			if(i!=0) cout<<"\t\t";
			else cout<<"\t";
			for(ll j=0;j<(ll)ans[i].size();j++){
				cout<<ans[i][j]<<" ";
			}
			if(i!=(ll)ans.size()-1)
			cout<<endl;
		}
}
#define deb(...) {cout<<" [ "#__VA_ARGS__<<" : "; print1(__VA_ARGS__); cout<<"]"<<endl; }
#define vdeb(...) {cout<<" [ "#__VA_ARGS__<<" : "; print2(__VA_ARGS__); cout<<"]"<<endl;}

// to return everything in string
#define ret(...) {stringstream returnn;returnn<<__VA_ARGS__;return returnn.str();}

stringstream retvec;
template<typename... T>
void returnvector(T... t){
		vector<vector<ll>> ans={t...};
		for(ll i=0;i<(ll)ans.size();i++){
			for(ll j=0;j<(ll)ans[i].size();j++){
				retvec<<ans[i][j];
				if(j!=(ll)ans[i].size()-1)
					retvec<<" ";
			}
			if(i!=(ll)ans.size()-1)
				retvec<<endl;
		}
}
#define vret(...) {returnvector(__VA_ARGS__);return retvec.str();}

//for loop
#define fo(i,n) for(int i=0;i<(ll)n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)

//to print vector element
template<typename T>
void printV(T vec){
	fo(i,vec.size()) cout<<vec[i]<<" "<<flush;
	cout<<endl;
	}

#define M ll(998244353)
const ll INF=ll(1e9+5);

//iterate map
template<typename T>
void itR(T map){
	 for(auto itr=map.begin();itr!=map.end();itr++){
				deb(itr->first,itr->second);
			}
	cout<<endl;
}

//c++ template for help.....................................................................................................................................................

string solve(){
	ll n;
	cin>>n;

	vector<ll> vec(n);
	fo(i,n) {cin>>vec[i];vec[i]--;}

	vector<ll> ans(n);
	ll last=0;
	for(int i=0;i<n;i++){
		//deb(i);
		set<ll> s;
		ll ti=i;
		while(s.count(ti)==0 and ans[ti]==0){
			//deb(ti);
			s.insert(ti);
			if(ti==vec[ti]) break;
			ti=vec[ti];
		}
		for(auto x:s){
			ans[x]=s.size();
			last+=s.size();
		}

	}

	map<ll,ll> mp;
	sort(ans.begin(),ans.end(),greater<ll>());
	fo(i,n){
		mp[ans[i]]++;
	}

	//vdeb(ans);
	if(mp[ans[0]]==ans[0]){
		if(ans[0]==ans.size()) ret(last);
		ll a=ans[0],b=-1;
		for(ll i=1;i<n;i++){
			if(ans[i]!=ans[i-1]){
				b=ans[i];
				break;
			}
		}
		if(b==-1) ret(last);
		ret(last+(2*a*b));
	}else{
		//deb(ans[0],mp[ans[0]],last);
		ret(last+ans[0]*mp[ans[0]]);
	}

	ret("");
}

int main() {
	cout<<setprecision(10);
	ios::sync_with_stdio(0);
	cin.tie(0);

	//ll testCase;
	//cin>>testCase;
	//for(ll tt=0;tt<testCase;tt++){
		string ans1=solve();
		if(ans1!="")
			cout<<ans1<<endl;
	//}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

//c++ templte for help.......................................................................................................................................................

typedef long long ll;

template<typename... T>
void print1(T... t){
	((cout<<t<<" "),...);
}

template<typename... T>
void print2(T... t){
		vector<vector<ll>> ans={t...};
		for(ll i=0;i<(ll)ans.size();i++){
			if(i!=0) cout<<"\t\t";
			else cout<<"\t";
			for(ll j=0;j<(ll)ans[i].size();j++){
				cout<<ans[i][j]<<" ";
			}
			if(i!=(ll)ans.size()-1)
			cout<<endl;
		}
}
#define deb(...) {cout<<" [ "#__VA_ARGS__<<" : "; print1(__VA_ARGS__); cout<<"]"<<endl; }
#define vdeb(...) {cout<<" [ "#__VA_ARGS__<<" : "; print2(__VA_ARGS__); cout<<"]"<<endl;}

// to return everything in string
#define ret(...) {stringstream returnn;returnn<<__VA_ARGS__;return returnn.str();}

stringstream retvec;
template<typename... T>
void returnvector(T... t){
		vector<vector<ll>> ans={t...};
		for(ll i=0;i<(ll)ans.size();i++){
			for(ll j=0;j<(ll)ans[i].size();j++){
				retvec<<ans[i][j];
				if(j!=(ll)ans[i].size()-1)
					retvec<<" ";
			}
			if(i!=(ll)ans.size()-1)
				retvec<<endl;
		}
}
#define vret(...) {returnvector(__VA_ARGS__);return retvec.str();}

//for loop
#define fo(i,n) for(int i=0;i<(ll)n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)

//to print vector element
template<typename T>
void printV(T vec){
	fo(i,vec.size()) cout<<vec[i]<<" "<<flush;
	cout<<endl;
	}

#define M ll(998244353)
const ll INF=ll(1e9+5);

//iterate map
template<typename T>
void itR(T map){
	 for(auto itr=map.begin();itr!=map.end();itr++){
				deb(itr->first,itr->second);
			}
	cout<<endl;
}

//c++ template for help.....................................................................................................................................................

string solve(){
	ll n;
	cin>>n;

	vector<ll> vec(n);
	fo(i,n) {cin>>vec[i];vec[i]--;}

	vector<ll> ans(n);
	ll last=0;
	for(int i=0;i<n;i++){
		//deb(i);
		set<ll> s;
		ll ti=i;
		while(s.count(ti)==0 and ans[ti]==0){
			//deb(ti);
			s.insert(ti);
			if(ti==vec[ti]) break;
			ti=vec[ti];
		}
		for(auto x:s){
			ans[x]=s.size();
			last+=s.size();
		}

	}

	map<ll,ll> mp;
	sort(ans.begin(),ans.end(),greater<ll>());
	fo(i,n){
		mp[ans[i]]++;
	}

	//vdeb(ans);
	if(mp[ans[0]]==ans[0]){
		ll a=ans[0],b=-1;
		for(ll i=1;i<n;i++){
			if(ans[i]!=ans[i-1]){
				b=ans[i];
				break;
			}
		}
		if(b==-1) ret(last);
		ret(last+(2*a*b));
	}else{
		//deb(ans[0],mp[ans[0]],last);
		ret(last+ans[0]*mp[ans[0]])
	}

	ret("");
}

int main() {
	cout<<setprecision(10);
	ios::sync_with_stdio(0);
	cin.tie(0);

	//ll testCase;
	//cin>>testCase;
	//for(ll tt=0;tt<testCase;tt++){
		string ans1=solve();
		if(ans1!="")
			cout<<ans1<<endl;
	//}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

//c++ templte for help.......................................................................................................................................................

typedef long long ll;

template<typename... T>
void print1(T... t){
	((cout<<t<<" "),...);
}

template<typename... T>
void print2(T... t){
		vector<vector<ll>> ans={t...};
		for(ll i=0;i<(ll)ans.size();i++){
			if(i!=0) cout<<"\t\t";
			else cout<<"\t";
			for(ll j=0;j<(ll)ans[i].size();j++){
				cout<<ans[i][j]<<" ";
			}
			if(i!=(ll)ans.size()-1)
			cout<<endl;
		}
}
#define deb(...) {cout<<" [ "#__VA_ARGS__<<" : "; print1(__VA_ARGS__); cout<<"]"<<endl; }
#define vdeb(...) {cout<<" [ "#__VA_ARGS__<<" : "; print2(__VA_ARGS__); cout<<"]"<<endl;}

// to return everything in string
#define ret(...) {stringstream returnn;returnn<<__VA_ARGS__;return returnn.str();}

stringstream retvec;
template<typename... T>
void returnvector(T... t){
		vector<vector<ll>> ans={t...};
		for(ll i=0;i<(ll)ans.size();i++){
			for(ll j=0;j<(ll)ans[i].size();j++){
				retvec<<ans[i][j];
				if(j!=(ll)ans[i].size()-1)
					retvec<<" ";
			}
			if(i!=(ll)ans.size()-1)
				retvec<<endl;
		}
}
#define vret(...) {returnvector(__VA_ARGS__);return retvec.str();}

//for loop
#define fo(i,n) for(int i=0;i<(ll)n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)

//to print vector element
template<typename T>
void printV(T vec){
	fo(i,vec.size()) cout<<vec[i]<<" "<<flush;
	cout<<endl;
	}

#define M ll(998244353)
const ll INF=ll(1e9+5);

//iterate map
template<typename T>
void itR(T map){
	 for(auto itr=map.begin();itr!=map.end();itr++){
				deb(itr->first,itr->second);
			}
	cout<<endl;
}

//c++ template for help.....................................................................................................................................................

string solve(){
	ll n;
	cin>>n;

	vector<ll> vec(n);
	fo(i,n) {cin>>vec[i];vec[i]--;}

	vector<ll> ans(n);
	ll last=0;
	for(int i=0;i<n;i++){
		//deb(i);
		set<ll> s;
		ll ti=i;
		while(s.count(ti)==0 and ans[ti]==0){
			//deb(ti);
			s.insert(ti);
			if(ti==vec[ti]) break;
			ti=vec[ti];
		}
		for(auto x:s){
			ans[x]=s.size();
			last+=s.size();
		}

	}

	map<ll,ll> mp;
	sort(ans.begin(),ans.end(),greater<ll>());
	fo(i,n){
		mp[ans[i]]++;
	}

	//vdeb(ans);
	if(mp[ans[0]]==ans[0]){
		if(ans[0]==(ll)ans.size()) ret(last);
		ll a=ans[0],b=ans[ans[0]];
		ret(last+(2*a*b));
	}else{
		//deb(ans[0],mp[ans[0]],last);
		ret(last+ans[0]*2*ans[0]);
	}

	ret("");
}

int main() {
	cout<<setprecision(10);
	ios::sync_with_stdio(0);
	cin.tie(0);

	//ll testCase;
	//cin>>testCase;
	//for(ll tt=0;tt<testCase;tt++){
		string ans1=solve();
		if(ans1!="")
			cout<<ans1<<endl;
	//}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mkp make_pair
#define ff first
#define PLL pair<ll,ll>
#define ss second
#define precision(x,d) cout<<fixed<<setprecision(d)<<x
#define minQueue priority_queue<ll,vector<ll>,greater<ll> >
#define maxQueue priority_queue<ll,vector<ll>,less<ll> >
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define F(i,a,b) for(ll i=a;i<b;i++)
#define RF(i,a,b) for(ll i=a;i>b;i--)
#define mset(a,b) memset((a),(b),sizeof((a)))
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
#define MOD 1000000007
#define RMOD 998244353
#define INF 10000000000000000
#define MINF LONG_LONG_MIN
#define MAXF LONG_LONG_MAX
#define OuterLoop ll t;cin>>t;while(t--)

vector<ll> v[3000000];

ll vis[3000000]={0};
ll cnt=0;

ll DFS(ll n)
{
    vis[n]=1;
    cnt++;
    for(auto x:v[n])
    {
        if(!vis[x])
        {
            DFS(x);
        }
    }

}

int main()
{
    ll n;
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        ll x;
        cin>>x;
        v[x].pb(i);
        v[i].pb(x);
    }

    vector<ll> pappu;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            cnt=0;
            DFS(i);
            pappu.pb(cnt);
        }
    }

      sort(allr(pappu));

      if(pappu.size()==1)
      {
          cout<<pappu[0]*pappu[0];
          return 0;
      }
      pappu[1]+=pappu[0];
      ll sum=0;
      for(int i=1;i<pappu.size();i++)
      {
          sum+= pappu[i]*pappu[i];
      }

      cout<<sum;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mkp make_pair
#define ff first
#define PLL pair<ll,ll>
#define ss second
#define precision(x,d) cout<<fixed<<setprecision(d)<<x
#define minQueue priority_queue<ll,vector<ll>,greater<ll> >
#define maxQueue priority_queue<ll,vector<ll>,less<ll> >
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define F(i,a,b) for(ll i=a;i<b;i++)
#define RF(i,a,b) for(ll i=a;i>b;i--)
#define mset(a,b) memset((a),(b),sizeof((a)))
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
#define MOD 1000000007
#define RMOD 998244353
#define INF 10000000000000000
#define MINF LONG_LONG_MIN
#define MAXF LONG_LONG_MAX
#define OuterLoop ll t;cin>>t;while(t--)

vector<ll> v[3000000];

ll vis[3000000]={0};
ll cnt=0;

ll DFS(ll n)
{
    vis[n]=1;
    cnt++;
    for(auto x:v[n])
    {
        if(!vis[x])
        {
            DFS(x);
        }
    }

}

int main()
{
    ll n;
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        ll x;
        cin>>x;
        v[x].pb(i);
        v[i].pb(x);
    }

    vector<ll> pappu;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            cnt=0;
            DFS(i);
            pappu.pb(cnt);
        }
    }

      sort(allr(pappu));
      pappu[1]+=pappu[0];
      ll sum=0;
      for(int i=1;i<pappu.size();i++)
      {
          sum+= pappu[i]*pappu[i];
      }

      cout<<sum;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mkp make_pair
#define ff first
#define PLL pair<ll,ll>
#define ss second
#define precision(x,d) cout<<fixed<<setprecision(d)<<x
#define minQueue priority_queue<ll,vector<ll>,greater<ll> >
#define maxQueue priority_queue<ll,vector<ll>,less<ll> >
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define F(i,a,b) for(ll i=a;i<b;i++)
#define RF(i,a,b) for(ll i=a;i>b;i--)
#define mset(a,b) memset((a),(b),sizeof((a)))
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
#define MOD 1000000007
#define RMOD 998244353
#define INF 10000000000000000
#define MINF LONG_LONG_MIN
#define MAXF LONG_LONG_MAX
#define OuterLoop ll t;cin>>t;while(t--)

vector<ll> v[3000000];

ll vis[3000000]={0};
ll cnt=0;

ll DFS(ll n)
{
    vis[n]=1;
    cnt++;
    for(auto x:v[n])
    {
        if(!vis[x])
        {
            DFS(x);
        }
    }

}

int main()
{
    ll n;
    cin>>n;

    for(int i=1;i<=n;i++)
    {
        ll x;
        cin>>x;
        v[x].pb(i);
        v[i].pb(x);
    }

    vector<ll> pappu;
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            cnt=0;
            DFS(i);
            pappu.pb(cnt);
        }
    }

      sort(allr(pappu));

      if(pappu.size()==1)
      {
          cout<<pappu[0];
          return 0;
      }
      pappu[1]+=pappu[0];
      ll sum=0;
      for(int i=1;i<pappu.size();i++)
      {
          sum+= pappu[i]*pappu[i];
      }

      cout<<sum;

}


/***
	chauhan176's code

	DO WHATEVER IT TAKES!!!
	Nothing happens , nothing happens and then everything happens.
	But the thing is nothing happens in air:)-
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define mod 1000000007
#define maxn 100005
#define lop(i,b) for(int i=0;i<b;i++)
#define lp(i,a,b) for(int i=a;i<b;i++)
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii;
const ll inf = 0x3f3f3f3f3f3f3f3fll;//4x10e18
vector<int> adj[maxn];
vector<int> vis(maxn,false);
vector<int>indeg(maxn,0);
vector<ll>ans;
int n,m,k;
ll mx,last;
map<int,int>mp;
set<int>st;
void dfs(int v)
{
    vis[v]=true;
    mx++;
    for(int p : adj[v])
    {
        if(!vis[p]){
            dfs(p);
        }
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output.txt", "w", stdout);
    #endif

    fastio;
    cin>>n;

    lp(i,1,n+1)
    {
        int x;
        cin>>x;

        adj[i].pb(x);
    }
    lp(i,1,n+1)
    {
        mx=0;
        if(!vis[i])
            dfs(i);
        ans.pb(mx);
    }
    ans.pb(0);ans.pb(0);
    sort(ans.begin(),ans.end(),greater<int>());
    ans[1]=ans[0]+ans[1];
    last=0;
    for(int i=1;;i++)
    {
        last+=ans[i]*ans[i];
        if(ans[i]==0)
            break;
    }
    cout<<last<<endl;
    return 0;
}


/***
	chauhan176's code

	DO WHATEVER IT TAKES!!!
	Nothing happens , nothing happens and then everything happens.
	But the thing is nothing happens in air:)-
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define mod 1000000007
#define maxn 100005
#define lop(i,b) for(int i=0;i<b;i++)
#define lp(i,a,b) for(int i=a;i<b;i++)
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii;
const ll inf = 0x3f3f3f3f3f3f3f3fll;//4x10e18
vector<int> adj[maxn];
vector<int> vis(maxn,false);
vector<int>indeg(maxn,0);
vector<int>ans;
int n,m,k;
ll mx,last;
map<int,int>mp;
set<int>st;
void dfs(int v)
{
    vis[v]=true;
    mx++;
    for(int p : adj[v])
    {
        if(!vis[p]){
            dfs(p);
        }
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output.txt", "w", stdout);
    #endif

    fastio;
    cin>>n;

    lp(i,1,n+1)
    {
        int x;
        cin>>x;

        adj[i].pb(x);
    }
    lp(i,1,n+1)
    {
        mx=0;
        if(!vis[i])
            dfs(i);
        ans.pb(mx);
    }
    ans.pb(0);ans.pb(0);
    sort(ans.begin(),ans.end(),greater<int>());
    ans[1]=ans[0]+ans[1];
    last=0;
    for(int i=1;;i++)
    {
        last+=ans[i]*ans[i];
        if(ans[i]==0)
            break;
    }
    cout<<last<<endl;
    return 0;
}


/***
	chauhan176's code

	DO WHATEVER IT TAKES!!!
	Nothing happens , nothing happens and then everything happens.
	But the thing is nothing happens in air:)-
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define mod 1000000007
#define maxn 100005
#define lop(i,b) for(int i=0;i<b;i++)
#define lp(i,a,b) for(int i=a;i<b;i++)
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii;
const ll inf = 0x3f3f3f3f3f3f3f3fll;//4x10e18
vector<int> adj[maxn];
vector<int> vis(maxn,false);
vector<int>indeg(maxn,0);
vector<int>ans;
int n,m,k;
ll mx,last;
map<int,int>mp;
set<int>st;
void dfs(int v)
{
    vis[v]=true;
    mx++;
    for(int p : adj[v])
    {
        if(!vis[p]){
            dfs(p);
        }
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output.txt", "w", stdout);
    #endif

    fastio;
    cin>>n;

    lp(i,1,n+1)
    {
        int x;
        cin>>x;

        adj[i].pb(x);
    }
    lp(i,1,n+1)
    {
        mx=0;
        if(!vis[i])
            dfs(i);
        ans.pb(mx);
    }
    sort(ans.begin(),ans.end(),greater<int>());
    ans[1]=ans[0]+ans[1];
    last=0;
    for(int i=1;;i++)
    {
        last+=ans[i]*ans[i];
        if(ans[i]==0)
            break;
    }
    cout<<last<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    long long n, answer = 0;
    cin >> n;
    int p[n];
    for (int i = 0; i < n; p[i++]--) {
        cin >> p[i];
    }
    vector<bool> visited(n);
    vector<int> loops;
    for (int i = 0; i < n; i++) {
        if (!visited[i]) {
            for (int j = i, k = 0; true; j = p[j], k++) {
                if (visited[j]) {
                    loops.push_back(k);
                    break;
                } else {
                    visited[j] = true;
                }
            }
        }
    }
    sort(loops.rbegin(),loops.rend());
    for (int v : loops) {
        answer += 1ll*v*v;
    }
    answer += 2ll*loops[0]*loops[1];
    cout << answer;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    long long n, answer = 0;
    cin >> n;
    int p[n];
    for (int i = 0; i < n; p[i++]--) {
        cin >> p[i];
    }
    vector<bool> visited(n);
    vector<int> loops;
    for (int i = 0; i < n; i++) {
        if (!visited[i]) {
            for (int j = i, k = 0; true; j = p[j], k++) {
                if (visited[j]) {
                    loops.push_back(k);
                    break;
                } else {
                    visited[j] = true;
                }
            }
        }
    }
    sort(loops.rbegin(),loops.rend());
    for (int v : loops) {
        answer += 1ll*v*v;
    }
    if (loops.size() >= 2) {
        answer += 2ll*loops[0]*loops[1];
    }
    cout << answer;
}


#include <bits/stdc++.h>
using namespace std;
long long sum(int x) {
    return 1ll*x*(x+1)/2;
}
long long square(int x) {
    return 1ll*x*x;
}
int main() {
    long long n, m = 0, answer;
    cin >> n;
    int p[n];
    for (int i = 0; i < n; p[i++]--) {
        cin >> p[i];
    }
    vector<bool> visited(n);
    vector<int> lines, loops;
    for (int i = 0; i < n; i++) {
        if (!visited[i]) {
            for (int j = i, k = 0; true; j = p[j], k++) {
                if (visited[j] && p[j] == j) {
                    lines.push_back(k);
                    break;
                } else if (visited[j]) {
                    loops.push_back(k);
                    break;
                } else {
                    visited[j] = true;
                }
            }
        }
    }
    sort(lines.rbegin(),lines.rend());
    sort(loops.rbegin(),loops.rend());
    for (int v : lines) {
        m += sum(v);
    }
    for (int v : loops) {
        m += square(v);
    }
    answer = m;
    if (lines.size() >= 3) {
        answer = max(answer,m-sum(lines[0])-sum(lines[1])-sum(lines[2])+sum(lines[0]+lines[1]+lines[2]));
    }
    if (lines.size() >= 2) {
        answer = max(answer,m-sum(lines[0])-sum(lines[1])+square(lines[0]+lines[1]));
    }
    if (loops.size() >= 2) {
        answer = max(answer,m-square(loops[0])-square(loops[1])+square(loops[0]+loops[1]));
    }
    if (loops.size() && lines.size()) {
        answer = max(answer,m-square(loops[0])-sum(lines[0])+square(loops[0]+lines[0]));
    }
    cout << answer;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    int n; cin >> n;
    int a[n];
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    bool visit[n+1];
    memset(visit, 0, sizeof(visit));
    vector<int> cyl;
    for(int i = 1; i <= n; i++)
    {
        int x = i, cnt = 0;
        while(!visit[x])
        {
            cnt++;
            visit[x] = true;
            x = a[x];
        }
        if(cnt) cyl.push_back(cnt);
    }
    sort(cyl.begin(), cyl.end());

    int m = cyl.size()-1;
    ll ans = 0;
    if(m > 0) ans += (ll(cyl[m]) + cyl[m-1]) *
        (ll(cyl[m]) + cyl[m-1]);
    cyl.pop_back(); cyl.pop_back();

    for(auto &x: cyl)
        ans += ll(x) * x;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    int n; cin >> n;
    int a[n+1];
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    bool visit[n+1];
    memset(visit, 0, sizeof(visit));
    vector<ll> cyl;
    for(int i = 1; i <= n; i++)
    {
        int x = i, cnt = 0;
        while(!visit[x])
        {
            cnt++;
            visit[x] = true;
            x = a[x];
        }
        if(cnt) cyl.push_back(cnt);
    }
    sort(cyl.begin(), cyl.end());

    int m = cyl.size()-1;
    ll ans = 0;
    if(m > 0) ans += (cyl[m] + cyl[m-1]) *
        (cyl[m] + cyl[m-1]);
    cyl.pop_back(); cyl.pop_back();

    for(ll x: cyl)
        ans += x * x;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    int n; cin >> n;
    int a[n+1];
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    bool visit[n+1];
    memset(visit, 0, sizeof(visit));
    vector<int> cyl;
    for(int i = 1; i <= n; i++)
    {
        int x = i, cnt = 0;
        while(!visit[x])
        {
            cnt++;
            visit[x] = true;
            x = a[x];
        }
        if(cnt) cyl.push_back(cnt);
    }
    sort(cyl.begin(), cyl.end());

    int m = cyl.size()-1;
    ll ans = 0;
    if(m > 0) ans += (ll(cyl[m]) + cyl[m-1]) *
        (ll(cyl[m]) + cyl[m-1]);
    cyl.pop_back(); cyl.pop_back();

    for(auto &x: cyl)
        ans += ll(x) * x;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    int n; cin >> n;
    int a[n+1];
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    bool visit[n+1];
    memset(visit, 0, sizeof(visit));
    vector<ll> cyl;
    for(int i = 1; i <= n; i++)
    {
        int x = i, cnt = 0;
        while(!visit[x])
        {
            cnt++;
            visit[x] = true;
            x = a[x];
        }
        if(cnt) cyl.push_back(cnt);
    }
    sort(cyl.begin(), cyl.end());

    int m = cyl.size()-1;
    ll ans = 0;
    if(m > 0) ans += (cyl[m] + cyl[m-1]) *
        (cyl[m] + cyl[m-1]);
    cyl.pop_back(); cyl.pop_back();

    for(auto &x: cyl)
        ans += x * x;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    int n; cin >> n;
    int a[n+1];
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    bool visit[n+1];
    memset(visit, 0, sizeof(visit));
    vector<int> cyl;
    for(int i = 1; i <= n; i++)
    {
        int x = i, cnt = 0;
        while(!visit[x])
        {
            cnt++;
            visit[x] = true;
            x = a[x];
        }
        if(cnt) cyl.push_back(cnt);
    }
    sort(cyl.begin(), cyl.end());

    int m = cyl.size()-1;
    ll ans = 0;
    if(m > 0)
    {
        ans += (ll(cyl[m]) + cyl[m-1]) *
            (ll(cyl[m]) + cyl[m-1]);
        cyl.pop_back(); cyl.pop_back();
    }

    for(ll x: cyl)
        ans += x * x;
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<string>
#include<string.h>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<deque>
#include<queue>
#include<math.h>
#include<chrono>
#include<list>
#include<random>
#include<bitset>
#include<assert.h>
#include<ctime>
#include<cstdio>
using namespace std;
ifstream fi("test.txt");
ofstream fo("out.txt");
#define fast_input() {ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); }
#define ll long long
#define For(i,n) for (int(i) = 0; (i)<(n); (i)++)
#define For1(i,n) for (int(i) = 1; (i) <= (n); (i)++)
#define Forlr(i,l,r) for (int(i) = (l); (i) <= (r); (i)++)
#define Forrl(i,r,l) for (int(i) = (r); (i) >= (l); (i)--)
#define All(a) a.begin(), a.end()
#define pb push_back
#define pp pop_back
#define mp make_pair
#define PI 3.14159265
#define putno return cout <<"NO", 0;
#define putyes return cout <<"YES", 0;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<ll> vl;
typedef map<int, int> mii;
//const ll MOD = 1e9 + 7;
const ll base = 269;
const ll ool = LLONG_MAX;
const int ooi = 1 << 30;
const int N = 1e5 + 5;
//DSU
/*
int p[ N], r[N];
void init(int n) {
	For(i, n) {
		p[i] = i;
	}
}
int get(int x) {
	if (p[x] != x) {
		p[x] = get(p[x]);
	}
	return p[x];
}
bool uni(int x, int y) {
	x = get(x); y = get(y);
	if (x == y) {
		return false;
	}
	if (r[x] >= r[y]) {
		p[y] = x;
	}
	else {
		p[x] = y;
	}
	if (r[x] == r[y]) {
		r[x]++;
	}
	return true;
}
*/

// So mu
/*
int power(int x, unsigned int y, int p)
{
	int res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0)
	{
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res*x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x*x) % p;
	}
	return res;
}
*/

// ReadHexa
/*
bitset<16400> b[4101];
void readHexa()
{
	For(i, n)
	{
		string st;
		cin>> st;
		For(j, m/4)
		{
			int x;
			if (st[j] >= 'A' && st[j] <= 'F')
				x = st[j] - 'A' + 10;
			else x = st[j] - '0';
			for (int k = 4; k; --k, x >>= 1)
				b[i][j * 4 + k] = x & 1;
		}
	}
}
*/

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
MIKUNO - 15/02/1999
*/
int n, m, k, t, cnt, rs1;
//ll n, m, q, k, pos, t, cnt, rs1, check;
ll maxx = -1e9, minn = 1e9;
const int maxn = 40;
const ll MOD = 1e9;
int s;
int a[N];
bool Free[N];
void dfs(int u)
{
	cnt++;
	Free[u] = 1;
	if (!Free[a[u]])
		dfs(a[u]);
}
int main()
{
	cin >> n;
	For(i, n)
	{
		int x, y;
		cin >> a[i + 1];
	}
	vector<pii> find;
	ll rs = 0;
	For1(i, n)
	{
		cnt = 0;
		if (!Free[i])
		{
			dfs(i);
			find.pb(mp(cnt, i));
			rs += (ll)cnt*(ll)cnt;
		}
	}
	sort(All(find));
	if (find.size() > 1)
	{
		rs += 2ll * find.back().first*find[find.size() - 2].first;
	}
	cout << rs;
}


#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<string.h>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<iterator>
#define mem(a,b) memset(a,b,sizeof(a))
#define MOD 100000007
#define LL long long
#define INF 0x3f3f3f3f
const double pi = acos(-1.0);
const int Maxn=100010;
using namespace std;
inline int scan()
{
    int x=0,c=1;
    char ch=' ';
    while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
    while(ch=='-')c*=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
    return x*c;
}
int pre[Maxn];
int cnt[Maxn];
int ans[Maxn];
int find(int x){
	return x==pre[x]?x:pre[x]=find(pre[x]);
}
int main(){
	std::ios::sync_with_stdio(false);
	int n,a;
	int k=0;
	cin>>n;
	for(int i=1;i<=Maxn;i++){
		pre[i]=i;
		cnt[i]=1;
	}
	for(int i=1;i<=n;i++){
		cin>>a;

		int fi=find(i);
		int fa=find(a);
		if(fi!=fa){
			pre[fa]=fi;
			cnt[fi]+=cnt[fa];
		}else{
			ans[k++]=cnt[fi];
		}
	}
	LL sum=0;
	sort(ans,ans+k);
	if(k==1)
	sum=ans[k-1]*ans[k-1];
	else
	sum=(ans[k-1]+ans[k-2])*(ans[k-1]+ans[k-2]);
	bool flag=true;
	for(int i=k-3;i>=0;i--){
		sum+=ans[i]*ans[i];
		//cout<<ans[i]<<endl;
	}
	cout<<sum<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<string.h>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<iterator>
#define mem(a,b) memset(a,b,sizeof(a))
#define MOD 100000007
#define LL long long
#define INF 0x3f3f3f3f
const double pi = acos(-1.0);
const int Maxn=100010;
using namespace std;
inline int scan()
{
    int x=0,c=1;
    char ch=' ';
    while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
    while(ch=='-')c*=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
    return x*c;
}
int pre[Maxn];
LL cnt[Maxn];
LL ans[Maxn];
int find(int x){
	return x==pre[x]?x:pre[x]=find(pre[x]);
}
int main(){
	std::ios::sync_with_stdio(false);
	int n,a;
	int k=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		pre[i]=i;
		cnt[i]=1;
	}
	for(int i=1;i<=n;i++){
		cin>>a;

		int fi=find(i);
		int fa=find(a);
		if(fi!=fa){
			pre[fa]=fi;
			cnt[fi]+=cnt[fa];
		}else{
			ans[k++]=cnt[fi];
		}
	}
	LL sum=0;
	sort(ans,ans+k);
	if(k==1)
	sum=ans[k-1]*ans[k-1];
	else
	sum=(ans[k-1]+ans[k-2])*(ans[k-1]+ans[k-2]);
	bool flag=true;
	for(int i=k-3;i>=0;i--){
		sum+=ans[i]*ans[i];
		//cout<<ans[i]<<endl;
	}
	cout<<sum<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<string.h>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#include<iterator>
#define mem(a,b) memset(a,b,sizeof(a))
#define MOD 100000007
#define LL long long
#define INF 0x3f3f3f3f
const double pi = acos(-1.0);
const int Maxn=100010;
using namespace std;
inline int scan()
{
    int x=0,c=1;
    char ch=' ';
    while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
    while(ch=='-')c*=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
    return x*c;
}
int pre[Maxn];
int cnt[Maxn];
int ans[Maxn];
int find(int x){
	return x==pre[x]?x:pre[x]=find(pre[x]);
}
int main(){
	std::ios::sync_with_stdio(false);
	int n,a;
	int k=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		pre[i]=i;
		cnt[i]=1;
	}
	for(int i=1;i<=n;i++){
		cin>>a;
		int fi=find(i);
		int fa=find(a);
		if(fi!=fa){
			pre[fa]=fi;
			cnt[fi]+=cnt[fa];
		}else{
			ans[k++]=cnt[fi];
		}
	}
	sort(ans,ans+k);
	LL sum=0;
	bool flag=true;
	for(int i=k-1;i>=0;i--){
		if(ans[i]>2){
			sum+=ans[i]*ans[i];
		}else if(flag&&i){
			sum+=(ans[i]+ans[i-1])*(ans[i]+ans[i-1]);
			i--;
			flag=false;
		}else{
			sum+=ans[i]*ans[i];
		}
		//cout<<ans[i]<<endl;
	}
	cout<<sum<<endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <iomanip>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long double ld;
typedef long long ll;
const int INF = 1000000000;
const int MAX_N = 200000;
int g[MAX_N];
int flag[MAX_N];
vector<ll> och;
int dfs(int v)
{
	if (flag[v] == 1)
		return 0;
	flag[v] = 1;
	int ans = 1;
	ans += dfs(g[v]);
	return ans;
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	ll n, t,a,ans;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> g[i];
		g[i]--;
	}
	ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (flag[i] == 0)
		{
			a = dfs(i);
			ans += a*a;
			och.push_back(a);
		}
	}
	sort(och.begin(),och.end());
	reverse(och.begin(), och.end());
	if (och.size() > 1)
	{
		ans += 2ll*och[0] * och[1];
	}
	cout << ans;
	return 0;
}


#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
const int N=100000+10;
bool vis[N];
LL cnt,n,tmp,p[N];

void dfs(int k)
{
    if(!vis[k])
        {
        vis[k]=1;
        cnt++;
        dfs(p[k]);
        }
}

int main()
{
    cin>>n;
    vector <LL> v;
    for(int i=0;i<n;i++)
    {
        cin>>tmp;
        p[i]=tmp-1;
    }
    for(int i=0;i<n;i++)
    {
        if(!vis[i])
        {
            cnt=0;
            dfs(i);
            v.push_back(cnt);
        }
    }
    if(v.size()==1) {cout<<v.back()*v.back()<<endl;return 0;}

    sort(v.begin(),v.end());
    tmp=v.back();v.pop_back();
    tmp+=v.back();v.pop_back();
    v.push_back(tmp);
    LL ans=0;
    for(int i=0;i<v.size();i++)
        ans+=v[i]*v[i];
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (auto v : cnt) {
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

3
2 3 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	cout << len(a, n);

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	if (cnt.size() == 1)
		return cnt[0] * cnt[0];
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	/*if (n == 1)
		cout << 1;
	else if (n == 2)
		cout << 4;
	else*/
	{
		cout << len(a, n);
	}

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	if (n == 1)
		cout << 1;
	else if (n == 2)
		cout << 4;
	else
		cout << len(a, n);

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	if (n == 1)
		cout << 2;
	else if (n == 2)
		cout << 4;
	else
		cout << len(a, n);

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

3
2 3 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	cout << len(a, n);

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	if (n == 1)
		cout << 1;
	else if (n == 2)
		cout << 43;
	else
		cout << len(a, n);

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<long long> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	if (cnt.size() == 1)
		return cnt[0] * cnt[0];
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	long long sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	cout << len(a, n);

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	if (n == 1)
		cout << 1;
	else if (n == 2)
		cout << 4;
	else
	{
		if (n < 10)
			cout << "fuck";
		cout << len(a, n);
	}

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	if (n == 1)
		cout << 1;
	else if (n == 2)
		cout << 4;
	else
	{
		if (n > 20)
			cout << "fuck";
		cout << len(a, n);
	}

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	if (cnt.size() == 1)
		return cnt[0] * cnt[0];
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	cout << len(a, n);

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int len(const int* a, const int &n) {
	vector<bool> vis(100000);
	vector<int> cnt;
	for (size_t i = 0; i < n; i++)
	{
		int j = i;
		int k = 0;
		while (!vis[j])
		{
			vis[j] = true;
			j = a[j];
			k++;
		}
		if (k != 0) cnt.push_back(k);
	}
	sort(cnt.begin(), cnt.end());
	cnt[cnt.size() - 2] += cnt[cnt.size() - 1];
	cnt.pop_back();

	int sum = 0;
	for (size_t i = 0; i < cnt.size(); i++) {
		auto v = cnt[i];
		sum += v*v;
	}

	return sum;
}

/*
3
2 1 3

2
2 1
*/

void main() {
	int n;
	cin >> n;
	int a[100000];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
	}

	if (n == 1)
		cout << 1;
	else if (n == 2)
		cout << 4;
	else
	{
		if (n > 10)
			cout << "fuck";
		cout << len(a, n);
	}

#if _DEBUG
	cout << "\n\n";
	system("pause");
#endif
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sqr(a) ((a)*(a))
#define bs ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define el "\n"
#define y1 y123

using namespace std;

void file()
{
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);
}

int n,kol1,kol2,ans,ans1;

string s;

int main()
{

    //bs;
    //file();
    cin>>n;
    cin>>s;
    for(int i=0;i<s.length();++i)
    {
        if(s[i]=='r'&&i%2==0)kol1++;
        if(s[i]=='b'&&i%2==1)kol2++;
    }
    ans+=min(kol1,kol2);
    kol1-=ans;
    kol2-=ans;
    ans+=kol1;
    ans+=kol2;

    kol1=0;
    kol2=0;

    for(int i=0;i<s.length();++i)
    {
        if(s[i]=='r'&&i%2==1)kol1++;
        if(s[i]=='b'&&i%2==0)kol2++;
    }
    ans1+=min(kol1,kol2);
    kol1-=ans1;
    kol2-=ans1;
    ans1+=kol1;
    ans1+=kol2;
    cout<<min(ans,ans1);
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sqr(a) ((a)*(a))
#define bs ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define el "\n"
#define y1 y123

using namespace std;

void file()
{
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);
}

int n,a[200001],kolc,kolb,ans,ans1,ans2,kolc1,kolb1;

string s;

int main()
{

    //bs;
    //file();
    cin>>n;
    cin>>s;
    for(int i=1;i<=n;++i)
    {
        if(s[i-1]=='r')
        {
            a[i]=1;
        }
    }

    for(int i=2;i<=n;++i)
    {
        if(a[i]==a[i-1]&&a[i]==1)
        {
            kolc++;
        }else if(a[i]==a[i-1])
        {
            kolb++;
        }
    }

    ans+=min(kolc,kolb);

    kolc-=ans;
    kolb-=ans;

    kolc1=kolc;
    kolb1=kolb;

    if(kolc)
    {
        if(a[1]==0)
        {
            kolc--;
            ans++;
        }
    }

    ans1=ans;

        if(kolc)
        {
            if(a[n]==0)
            {
                kolc--;
                ans1++;
            }
        }

        if(kolb)
        {
            if(a[1]==1)
            {
                kolb--;
                ans1++;
            }
        }

        if(kolb)
        {
            if(a[n]==1)
            {
                kolb--;
                ans1++;
            }
        }

        ans1+=kolb/2;
        ans1+=kolc/2;

        if(kolb==1)
        {
            ans1++;
        }

        if(kolc==1)
        {
            ans1++;
        }

    ans2=ans;

    kolc=kolc1;
    kolb=kolb1;

    ans2+=kolb/2;
    ans2+=kolc/2;

    if(kolb==1)
    {
        ans2++;
    }

    if(kolc==1)
    {
        ans2++;
    }

    cout<<min(ans1,ans2);

}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sqr(a) ((a)*(a))
#define bs ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define el "\n"
#define y1 y123

using namespace std;

void file()
{
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);
}

int n,a[200001],kolc,kolb,ans,ans1,ans2,ans3,kolc1,kolb1;

string s;

int main()
{

    //bs;
    //file();
    cin>>n;
    cin>>s;
    for(int i=1;i<=n;++i)
    {
        if(s[i-1]=='r')
        {
            a[i]=1;
        }
    }

    for(int i=2;i<=n;++i)
    {
        if(a[i]==a[i-1]&&a[i]==1)
        {
            kolc++;
        }else if(a[i]==a[i-1])
        {
            kolb++;
        }
    }

    ans+=min(kolc,kolb);

    kolc-=ans;
    kolb-=ans;

    cout<<(kolc+1)/2+(kolb+1)/2+ans;
    return 0;

    ans3+=(kolb+1)/2;
    ans3+=(kolc+1)/2;

    kolc1=kolc;
    kolb1=kolb;

    ans1=ans;

    if(kolc)
    {
        if(a[1]==0)
        {
            kolc--;
            ans1++;
        }
    }

    if(kolc)
    {
        if(a[n]==0)
        {
            kolc--;
            ans1++;
        }
    }

    if(kolb)
    {
        if(a[1]==1)
        {
            kolb--;
            ans1++;
        }
    }

    if(kolb)
    {
        if(a[n]==1)
        {
            kolb--;
            ans1++;
        }
    }

    ans1+=(kolb+1)/2;
    ans1+=(kolc+1)/2;

    ans2=ans;

    kolc=kolc1;
    kolb=kolb1;

    ans2+=(kolb+1)/2;
    ans2+=(kolc+1)/2;

    cout<<min(min(ans1,ans2),ans3);

}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sqr(a) ((a)*(a))
#define bs ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define el "\n"
#define y1 y123

using namespace std;

void file()
{
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);
}

int n,a[200001],kolc,kolb,ans,ans1,ans2,ans3,kolc1,kolb1,k1,k2;

string s;

int main()
{

    //bs;
    //file();
    cin>>n;
    cin>>s;
    for(int i=1;i<=n;++i)
    {
        if(s[i-1]=='r')
        {
            a[i]=1;
        }
    }

    for(int i=2;i<=n;++i)
    {
        if(a[i]==a[i-1]&&a[i]==1)
        {
            kolc++;
        }else if(a[i]==a[i-1])
        {
            kolb++;
        }
    }

    ans1=(kolc+1)/2+(kolb+1)/2;

    cout<<min((kolc+1)/2+(kolb+1)/2,max(kolc,kolb)/2);
    return 0;

    ans3+=(kolb+1)/2;
    ans3+=(kolc+1)/2;

    kolc1=kolc;
    kolb1=kolb;

    ans1=ans;

    if(kolc)
    {
        if(a[1]==0)
        {
            kolc--;
            ans1++;
        }
    }

    if(kolc)
    {
        if(a[n]==0)
        {
            kolc--;
            ans1++;
        }
    }

    if(kolb)
    {
        if(a[1]==1)
        {
            kolb--;
            ans1++;
        }
    }

    if(kolb)
    {
        if(a[n]==1)
        {
            kolb--;
            ans1++;
        }
    }

    ans1+=(kolb+1)/2;
    ans1+=(kolc+1)/2;

    ans2=ans;

    kolc=kolc1;
    kolb=kolb1;

    ans2+=(kolb+1)/2;
    ans2+=(kolc+1)/2;

    cout<<min(min(ans1,ans2),ans3);

}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sqr(a) ((a)*(a))
#define bs ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define el "\n"
#define y1 y123

using namespace std;

void file()
{
    freopen("input.in","r",stdin);
    freopen("output.out","w",stdout);
}

int n,a[200001],kolc,kolb,ans,ans1,ans2,ans3,kolc1,kolb1,k1,k2;

string s;

int main()
{

    //bs;
    //file();
    cin>>n;
    cin>>s;
    for(int i=1;i<=n;++i)
    {
        if(s[i-1]=='r')
        {
            a[i]=1;
        }
    }

    for(int i=2;i<=n;++i)
    {
        if(a[i]==a[i-1]&&a[i]==1)
        {
            kolc++;
        }else if(a[i]==a[i-1])
        {
            kolb++;
        }
    }

    ans1=(kolc+1)/2+(kolb+1)/2;

    cout<<min((kolc+1)/2+(kolb+1)/2,(kolc+kolb)/2);
    return 0;

    ans3+=(kolb+1)/2;
    ans3+=(kolc+1)/2;

    kolc1=kolc;
    kolb1=kolb;

    ans1=ans;

    if(kolc)
    {
        if(a[1]==0)
        {
            kolc--;
            ans1++;
        }
    }

    if(kolc)
    {
        if(a[n]==0)
        {
            kolc--;
            ans1++;
        }
    }

    if(kolb)
    {
        if(a[1]==1)
        {
            kolb--;
            ans1++;
        }
    }

    if(kolb)
    {
        if(a[n]==1)
        {
            kolb--;
            ans1++;
        }
    }

    ans1+=(kolb+1)/2;
    ans1+=(kolc+1)/2;

    ans2=ans;

    kolc=kolc1;
    kolb=kolb1;

    ans2+=(kolb+1)/2;
    ans2+=(kolc+1)/2;

    cout<<min(min(ans1,ans2),ans3);

}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
int main()
{
    int n,key;
    char t[1000500];
    int a[1000500];
    scanf("%d",&n);
    scanf("%s",t);
    for(int i=0;i<n;i++)
    {
       int z=i+1;
        if(t[i]=='r')
            a[z]=1;
        else
            a[z]=0;
    }
    int step=0;
    for(int i=2;i<=n;i++)
    {
        if(a[i]==0&&a[i-1]==1)
            continue;
        if(a[i]==0&&a[i-1]==0)
        {
            int z=i+1;
            if(z<=n&&a[z]==1)
            {
                step++;
                a[z]=0;
                a[z-1]=1;
            }
            else
            {
                step++;
                a[z-1]=1;
            }
            continue;
        }
        if(a[i]==1&&a[i-1]==0)
            continue;
        if(a[i]==1&&a[i-1]==1)
        {
            int z=i+1;
            if(z<=n&&a[z]==0)
            {
                step++;
                a[z]=1;
                a[z-1]=0;
            }
            else
            {
                step++;
                a[i]=0;
            }
            continue;
        }
    }
    printf("%d\n",step);
}
/*
15
rbrbrbrbbrbrbrb
*/


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<cstring>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#define cuttime ios::sync_with_stdio(false)
#define swap(x, y) x ^= y, y ^= x, x^= y
#define INF 0x3f3f3f3f
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define scan1(a) scanf("%d",&a)
#define lowbit(x) (x&(-x))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=1e5+5;
const int mod=100000007;
inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0',c=getchar();}
    return x*f;
}
inline void out(int x) {
	if(x>9) out(x/10);
	putchar(x%10+'0');
}
char s[N];
int main(){
	int n;
	int cnt1=0;
	int cnt2=0;
	cin>>n;
	cin>>s+1;
	int len=strlen(s+1);
	int cntb=0,cntr=0;
	rep(i,1,len){
		if(s[i]=='r')cntr++;
		else cntb++;
	}
	if(cntb==0){
		cout<<cntr/2;
		return 0;
	}
	if(cntr==0){
		cout<<cntb/2;
		return 0;
	}
	int ans=0;
	if(s[1]=='r'){
		rep(i,1,len){
			if(i%2==0){
				if(s[i]=='r')cnt1++;
			}
			if(i%2==1){
				if(s[i]=='b')cnt2++;
			}
		}
		if(cnt1==cnt2){
			ans+=cnt1;
		}
		else if(cnt1<cnt2){
			ans+=cnt1;
			cnt2=cnt2-cnt1;
			cnt1=0;
			ans+=cnt2;
		}
		else if(cnt1>cnt2){
			ans+=cnt2;
			cnt1=cnt1-cnt2;
			ans+=cnt1;
		}
		cout<<ans;
	}
	if(s[1]=='b'){
		rep(i,1,len){
			if(i%2==0){
				if(s[i]=='b')cnt1++;
			}
			if(i%2==1){
				if(s[i]=='r')cnt2++;
			}
		}
		if(cnt1==cnt2){
			ans+=cnt1;
		}
		else if(cnt1<cnt2){
			ans+=cnt1;
			cnt2=cnt2-cnt1;
			cnt1=0;
			ans+=cnt2;
		}
		else if(cnt1>cnt2){
			ans+=cnt2;
			cnt1=cnt1-cnt2;
			ans+=cnt1;
		}
		cout<<ans;
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5+10;
int main(){
	char s[maxn];
	int n;
	scanf("%d",&n);
	scanf("%s",s+1);
	int a = 0,b = 0;
	int c = 0,d = 0;
	for(int i = 1; i <= n; i++){
		if(s[i] == 'r' && i%2) a++;
		if(s[i] == 'r' && i%2==0) b++;
		if(s[i] == 'b' && i%2) c++;
		if(s[i] == 'b' && i%2==0)d++;
	}
	int flag = 0;
	int ma = max(a+d,b+c);
	if(a+d>b+c) flag = 1;
	if(ma == n){
		printf("0\n");
		return 0;
	}
	int ans = 0;
	if(flag){
		int x = min(b,c);
		ans+=x;
		b-=x;c-=x;
		int all = a+d+2*x;
		ans+=n-all;
	}else{
		int x = min(a,d);
		ans+=x;
		a-=x;d-=x;
		int all = b+c+2*x;
		ans+=n-all;
	}
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<cstring>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#define cuttime ios::sync_with_stdio(false)
#define swap(x, y) x ^= y, y ^= x, x^= y
#define INF 0x3f3f3f3f
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define scan1(a) scanf("%d",&a)
#define lowbit(x) (x&(-x))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N=1e5+5;
const int mod=100000007;
inline ll read() {
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0',c=getchar();}
    return x*f;
}
inline void out(int x) {
	if(x>9) out(x/10);
	putchar(x%10+'0');
}
char s[N];
int main(){
	int n;
	int cnt1=0;
	int cnt2=0;
	cin>>n;
	cin>>s+1;
	int len=strlen(s+1);
	int cntb=0,cntr=0;
	rep(i,1,len){
		if(s[i]=='r')cntr++;
		else cntb++;
	}
	if(s[1]=='r'){
		rep(i,1,len){
			if(i%2==0){
				if(s[i]=='r')cnt1++;
			}
			if(i%2==1){
				if(s[i]=='b')cnt1++;
			}
		}
		if(cntb==0){
			cout<<cnt1;
		}
		else cout<<cnt1/2+(cnt1%2==1);
	}
	if(s[1]=='b'){
		rep(i,1,len){
			if(i%2==0){
				if(s[i]=='b')cnt1++;
			}
			if(i%2==1){
				if(s[i]=='r')cnt1++;
			}
		}
		if(cntr==0){
			cout<<cnt1;
		}
		else cout<<cnt1/2+(cnt1%2==1);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn], b[maxn];
char c;

int judge()
{
  int swi1 = 0; int swi2 = 0;
  for(int i = 0; i + 2 < n; i++)
  {
    if(a[i] != a[i + 1]) continue;

    if(a[i + 2] != a[i + 1])
    {
      a[i + 1] = !a[i + 1];
      a[i + 2] = !a[i + 2];
      swi1++;
    }
    else
    {
      a[i + 1] = !a[i + 1];
      swi1++;
    }
  }

  if(a[n - 2] != a[n - 3] && a[n - 2] == a[n - 1])
  {
    swi1++;
  }
  // for(int i = 0; i < n; i++) cout << a[i];
  // printf("\n");

  for(int i = n - 1; i - 2 >= 0; i--)
  {
    if(b[i] != b[i - 1]) continue;

    if(b[i - 1] != b[i - 2])
    {
      b[i - 1] = !b[i - 1];
      b[i - 2] = !b[i - 2];
      swi2++;
    }
    else
    {
      b[i - 1] = !b[i - 1];
      swi2++;
    }
  }

  if(b[2] != b[1] && b[1] == b[0])
  {
    swi2++;
  }
  return swi1 < swi2 ? swi1 : swi2;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf("%c", &c);
    if(c == 'r') { a[i] = 0; b[i] = 0; }
    else { a[i] = 1; b[i] = 1; }
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn], b[maxn];
char c;

int judge()
{
  int w[2];
  int lmin; int rmin;
  w[0] = w[1] = 0;
  for(int i = 1; i < n; i++)
  {
    if(a[i] == a[i - 1])
    {
      w[a[i]]++;
      a[i] = !a[i];
    }
  }
  lmin = w[0] > w[1] ? w[0] : w[1];
  w[0] = w[1] = 0;
  // if(n & 1) return lmin;
  if(b[0] == a[0]) { w[b[0]]++; b[0] = !b[0]; }
  for(int i = 1; i < n; i++)
  {
    if(b[i] == b[i - 1])
    {
      w[b[i]]++;
      b[i] = !b[i];
    }
  }
  rmin = w[0] > w[1] ? w[0] : w[1];

  return rmin < lmin ? rmin : lmin;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf(" %c", &c);
    if(c == 'r') { a[i] = 0; b[i] = 0; }
    else { a[i] = 1; b[i] = 0; }
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn], b[maxn];
char c;

int judge()
{
  int w[2];
  int lmin; int rmin;
  w[0] = w[1] = 0;
  for(int i = 1; i < n; i++)
  {
    if(a[i] == a[i - 1])
    {
      w[a[i]]++;
      a[i] = !a[i];
    }
  }
  lmin = w[0] > w[1] ? w[0] : w[1];
  w[0] = w[1] = 0;
  for(int i = n - 1; i > 0; i--)
  {
    if(b[i + 1] == b[i])
    {
      w[b[i + 1]]++;
      b[i + 1] = !b[i + 1];
    }
  }
  rmin = w[0] > w[1] ? w[0] : w[1];

  return rmin < lmin ? rmin : lmin;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf(" %c", &c);
    if(c == 'r') { a[i] = 0; b[i] = 0; }
    else { a[i] = 1; b[i] = 0; }
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 100010;

int m;
char s[maxn];
int ans[2][2];

int main()
{
	scanf("%d", &m);
	scanf("%s", s);
	for(int i = 0; i < m; i++)
	{
		if(i & 1) { if(s[i] == 'b') ans[0][0]++; else ans[1][1]++; }
		else { if(s[i] == 'r') ans[0][1]++; else ans[1][0]++; }

	}
	cout << min(max(ans[0][0], ans[0][1]), max(ans[1][1], ans[1][0]));
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn];
char c;

int judge()
{
  int swi = 0;
  for(int i = 0; i + 2 < n; i++)
  {
    if(a[i] != a[i + 1]) continue;

    if(a[i + 2] != a[i + 1])
    {
      a[i + 1] = !a[i + 1];
      a[i + 2] = !a[i + 2];
      swi++;
    }
    else
    {
      a[i + 1] = !a[i + 1];
      swi++;
    }
  }

  return swi;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    cin >> c;
    if(c == 'r') a[i] = 0;
    else a[i] = 1;
  }
  cout << judge() << '\n';
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn];
char c;

int judge()
{
  int swi = 0;
  for(int i = 0; i + 2 < n; i++)
  {
    if(a[i] != a[i + 1]) continue;

    if(a[i + 2] != a[i + 1])
    {
      a[i + 1] = !a[i + 1];
      a[i + 2] = !a[i + 2];
      swi++;
    }
    else
    {
      a[i + 1] = !a[i + 1];
      swi++;
    }
  }

  if(a[n - 2] != a[n - 3] && a[n - 2] == a[n - 1])
  {
    a[n - 1] = !a[n - 1];
    a[n - 2] = !a[n - 2];
    swi++;
  }
  // for(int i = 0; i < n; i++) cout << a[i];
  // printf("\n");
  return swi;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf("%c", &c);
    if(c == 'r') a[i] = 0;
    else a[i] = 1;
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn], b[maxn];
char c;

int judge()
{
  int w[2];
  int lmin; int rmin;
  w[0] = w[1] = 0;
  for(int i = 1; i < n; i++)
  {
    if(a[i] == a[i - 1])
    {
      w[a[i]]++;
      a[i] = !a[i];
    }
  }
  lmin = w[0] > w[1] ? w[0] : w[1];
  w[0] = w[1] = 0;
  // if(n & 1) return lmin;
  w[b[0]]++;
  b[0] = !b[0];
  for(int i = 1; i < n; i++)
  {
    if(b[i] == b[i - 1])
    {
      w[b[i]]++;
      b[i] = !b[i];
    }
  }
  rmin = w[0] > w[1] ? w[0] : w[1];

  return (rmin < lmin ? rmin : lmin);
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf(" %c", &c);
    if(c == 'r') { a[i] = 0; b[i] = 0; }
    else { a[i] = 1; b[i] = 0; }
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn], b[maxn];
char c;

int judge()
{
  int w[2];
  int lmin; int rmin;
  w[0] = w[1] = 0;
  for(int i = 1; i < n; i++)
  {
    if(a[i] == a[i - 1])
    {
      w[a[i]]++;
      a[i] = !a[i];
    }
  }
  lmin = w[0] > w[1] ? w[0] : w[1];
  w[0] = w[1] = 0;
  for(int i = n - 1; i > 0; i--)
  {
    if(b[i - 1] == b[i])
    {
      w[b[i - 1]]++;
      b[i - 1] = !b[i - 1];
    }
  }
  rmin = w[0] > w[1] ? w[0] : w[1];

  return rmin < lmin ? rmin : lmin;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf(" %c", &c);
    if(c == 'r') { a[i] = 0; b[i] = 0; }
    else { a[i] = 1; b[i] = 0; }
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn], b[maxn];
char c;

int judge()
{
  int w[2];
  int lmin; int rmin;
  w[0] = w[1] = 0;
  for(int i = 1; i < n; i++)
  {
    if(a[i] == a[i - 1])
    {
      w[a[i]]++;
      a[i] = !a[i];
    }
  }
  lmin = w[0] > w[1] ? w[0] : w[1];
  w[0] = w[1] = 0;
  for(int i = n - 1; i > 0; i--)
  {
    if(b[i + 1] == b[i])
    {
      w[a[i + 1]]++;
      b[i + 1] = !b[i + 1];
    }
  }
  rmin = w[0] > w[1] ? w[0] : w[1];

  return rmin < lmin ? rmin : lmin;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf(" %c", &c);
    if(c == 'r') { a[i] = 0; b[i] = 0; }
    else { a[i] = 1; b[i] = 0; }
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn];
char c;

int judge()
{
  int swi = 0;
  for(int i = 0; i + 2 < n; i++)
  {
    if(a[i] != a[i + 1]) continue;

    if(a[i + 2] != a[i + 1])
    {
      a[i + 1] = !a[i + 1];
      a[i + 2] = !a[i + 2];
      swi++;
    }
    else
    {
      a[i + 1] = !a[i + 1];
      swi++;
    }
  }

  if(a[n - 2] != a[n - 3] && a[n - 2] == a[n - 1])
  {
    a[n - 1] = !a[n - 1];
    a[n - 2] = !a[n - 2];
    swi++;
  }
  // for(int i = 0; i < n; i++) cout << a[i];
  // printf("\n");
  return swi;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    cin >> c;
    if(c == 'r') a[i] = 0;
    else a[i] = 1;
  }
  cout << judge() << '\n';
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn], b[maxn];
char c;

int judge()
{
  // for(int i = 0; i < n; i++) printf("%d ", a[i]);
  // printf("\n");

  int swi1 = 0; int swi2 = 0;
  for(int i = 0; i + 2 < n; i++)
  {
    if(a[i] != a[i + 1]) continue;

    if(a[i + 2] != a[i + 1])
    {
      //printf("%d\n", i);
      a[i + 1] = !a[i + 1];
      a[i + 2] = !a[i + 2];
      swi1++;
    }
    else
    {
      //printf("%d\n", i);
      a[i + 1] = !a[i + 1];
      swi1++;
    }
  }

  if(a[n - 2] != a[n - 3] && a[n - 2] == a[n - 1])
  {
    a[n - 1] = !a[n - 1];
    //printf("%d\n", n - 3);
    swi1++;
  }
  // for(int i = 0; i < n; i++) cout << a[i];
  // printf("\n");
  //printf("A\n");
  for(int i = n - 1; i - 2 >= 0; i--)
  {
    if(b[i] != b[i - 1]) continue;

    if(b[i - 1] != b[i - 2])
    {
      b[i - 1] = !b[i - 1];
      b[i - 2] = !b[i - 2];
      swi2++;
    }
    else
    {
      b[i - 1] = !b[i - 1];
      swi2++;
    }
  }

  if(b[2] != b[1] && b[1] == b[0])
  {
    b[0] = !b[0];
    swi2++;
  }
  /*
  for(int i = 0; i < n; i++) printf("%d ", a[i]);
  printf("\n");
  for(int i = 0; i < n; i++) printf("%d ", b[i]);
  printf("\n");
  printf("%d %d\n", swi1, swi2);
  */
  return swi1 < swi2 ? swi1 : swi2;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf(" %c", &c);
    if(c == 'r') { a[i] = 0; b[i] = 0; }
    else { a[i] = 1; b[i] = 1; }
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

const int maxn = 100010;

int n;
int  a[maxn], b[maxn];
char c;

int judge()
{
  //for(int i = 0; i < n; i++) printf("%d ", a[i]);
  //printf("\n");

  int swi1 = 0; int swi2 = 0;
  for(int i = 0; i + 2 < n; i++)
  {
    if(a[i] != a[i + 1]) continue;

    if(a[i + 2] != a[i + 1])
    {
      //printf("%d\n", i);
      a[i + 1] = !a[i + 1];
      a[i + 2] = !a[i + 2];
      swi1++;
    }
    else
    {
      //printf("%d\n", i);
      a[i + 1] = !a[i + 1];
      swi1++;
    }
  }

  if(a[n - 2] != a[n - 3] && a[n - 2] == a[n - 1])
  {
    //printf("%d\n", n - 3);
    swi1++;
  }
  // for(int i = 0; i < n; i++) cout << a[i];
  // printf("\n");
  //printf("A\n");
  for(int i = n - 1; i - 2 >= 0; i--)
  {
    if(b[i] != b[i - 1]) continue;

    if(b[i - 1] != b[i - 2])
    {
      b[i - 1] = !b[i - 1];
      b[i - 2] = !b[i - 2];
      swi2++;
    }
    else
    {
      b[i - 1] = !b[i - 1];
      swi2++;
    }
  }

  if(b[2] != b[1] && b[1] == b[0])
  {
    swi2++;
  }

  //printf("%d %d\n", swi1, swi2);
  return swi1 < swi2 ? swi1 : swi2;
}

int main()
{
  cin >> n;
  for(int i = 0; i < n; i++)
  {
    scanf(" %c", &c);
    if(c == 'r') { a[i] = 0; b[i] = 0; }
    else { a[i] = 1; b[i] = 1; }
  }
  cout << judge();
  return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const char r = 'r';
const char b = 'b';

string s;

void swap_ch(const int ind) {
	if (s[ind] == r)
		s[ind] = b;
	else
		s[ind] = r;
}

int main() {
	int n;
	cin >> n;
	string d;
	cin >> d;
	if (n == 1) {
		cout << 0;
		return 0;
	}
	s = d;
	int cnt1(0);
	for (int i = 1; i < s.length(); ++i) {
		if (s[i] == s[i-1]) {
			cnt1++;
			if (s[i] == s[i+1])
				swap_ch(i);
			else {
				swap(s[i], s[i+1]);
			}
		}
	}
	s = d;
	int cnt2(1);
	if (s[0] == s[1])
		swap_ch(0);
	else
		swap(s[0], s[1]);
	for (int i = 1; i < s.length(); ++i) {
		if (s[i] == s[i-1]) {
			cnt2++;
			if (s[i] == s[i+1])
				swap_ch(i);
			else {
				swap(s[i], s[i+1]);
			}
		}
	}
	cout << min(cnt1, cnt2);
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const char r = 'r';
const char b = 'b';

string s;

void swap_ch(const int ind) {
	if (s[ind] == r)
		s[ind] = b;
	else
		s[ind] = r;
}

int main() {
	int n;
	cin >> n;
	string d;
	cin >> d;
	if (n == 1) {
		cout << 0;
		return 0;
	}
	else if (n == 2) {
		if (d[0] == d[1])
			cout << 1;
		else
			cout << 0;
		return 0;
	}
	s = d;
	int cnt1(0);
	for (int i = 1; i < s.length() - 1; ++i) {
		if (s[i] == s[i-1]) {
			cnt1++;
			if (s[i] == s[i+1])
				swap_ch(i);
			else {
				swap(s[i], s[i+1]);
			}
		}
	}
	s = d;
	int cnt2(1);
	if (s[0] == s[1])
		swap_ch(0);
	else
		swap(s[0], s[1]);
	for (int i = 1; i < s.length() - 1; ++i) {
		if (s[i] == s[i-1]) {
			cnt2++;
			if (s[i] == s[i+1])
				swap_ch(i);
			else {
				swap(s[i], s[i+1]);
			}
		}
	}
	cout << min(cnt1, cnt2);
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const char r = 'r';
const char b = 'b';

int main() {
	int n;
	cin >> n;
	string s;
	cin >> s;
	if (n == 1) {
		cout << 0;
		return 0;
	}
	int cnt1(0), cnt2(0);
	for (int i = 0; i < s.length(); i+=2) {
		if (s[i] != r) {
			cnt1++;
			if ((i+1 < s.length()) && (s[i+1] != r))
				cnt2++;
		}
		else {
			cnt2++;
			if ((i+1 < s.length()) && (s[i+1] != b))
				cnt1++;
		}
	}
	cout << min(cnt1, cnt2);
	return 0;
}