#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

set<int> detect_cycle(int N, const vector<vector<int>>& G)
{
	vector<int> stk;
	set<int> cycle;
	function<void(int,int)> rec = [&](int pre, int v) {
		stk.push_back(v);
		for(int u: G[v]) if(u != pre) {
			auto it = find(stk.begin(), stk.end(), u);
			if(it == stk.end()) {
				rec(v, u);
			} else {
				cycle = set<int>(it, stk.end());
			}
		}
		stk.pop_back();
	};
	rec(-1, 0);
	return cycle;
}

vector<vector<int>> flatten(const vector<set<int>>& GS)
{
	vector<vector<int>> G;
	for(const auto& s: GS)
		G.emplace_back(s.begin(), s.end());
	return G;
}

class SuccessfulMerger { public:
	int minimumMergers(vector <int> road)
	{
		const int N = road.size();
		vector<set<int>> Gs(N);
		for(int i=0; i<N; ++i) {
			Gs[i].insert(road[i]);
			Gs[road[i]].insert(i);
		}
		vector<vector<int>> G = flatten(Gs);

		set<int> cy = detect_cycle(N, G);
		if(cy.empty())
			return solve(N, G);

		int best = N-1;
		set<int> cyl = cy;
		for(int survive: cyl) {
			cy.erase(survive);

			map<int,int> remap;
			int XN = N - cy.size() + 1;
			for(int v=0; v<N; ++v) {
				if(cy.count(v)) {
					remap[v] = XN - 1;
				} else {
					int idx = 0;
					for(int u=0; u<v; ++u)
						if(!cy.count(u))
							++idx;
					remap[v] = idx;
				}
			}
			vector<set<int>> XGs(XN);
			for(int i=0; i<N; ++i) {
				int a = remap[i];
				int b = remap[road[i]];
				if(a != b) {
					XGs[a].insert(b);
					XGs[b].insert(a);
				}
			}
			vector<vector<int>> XG = flatten(XGs);
			best = min<int>(best, solve(XN, XG) + cy.size() - 1);

			cy.insert(survive);
		}
		return best;
	}

	int solve(int N, const vector<vector<int>>& G) {
		if(N <= 2)
			return 0;

		int best = N-1;
		for(int root=0; root<N; ++root)
			best = min(best, solve(N, root, G));
		return best;
	}

	int solve(int N, int root, const vector<vector<int>>& G)
	{
		function<int(int,int)> non_leaf_cnt = [&](int pre, int v) {
			if(G[v].size() == 1)
				return 0;
			int sum = 1;
			for(int u: G[v]) if(u != pre)
				sum += non_leaf_cnt(v, u);
			return sum;
		};

		int total = 0;
		for(int ch: G[root])
			total += non_leaf_cnt(root, ch);
		return total;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, SuccessfulMerger().minimumMergers(road));}
int main(){

CASE(0)
	int road_[] = {2, 2, 1, 1, 1};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	int _ = 1; 
END
CASE(1)
	int road_[] = {3, 4, 5, 4, 5, 3};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	int _ = 2; 
END
CASE(2)
	int road_[] = {1, 0, 1, 0, 0, 0, 1, 0, 1, 1};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	int _ = 1; 
END
CASE(3)
	int road_[] = {1, 2, 3, 0};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	int _ = 2; 
END
CASE(4)
	int road_[] = {31, 19, 0, 15, 30, 32, 15, 24, 0, 20, 40, 1, 22, 21, 39, 28, 0, 23, 15, 5, 19, 22, 6, 32, 8, 38, 35, 30, 4, 28, 32, 18, 18, 9, 22, 41, 20, 18, 6, 25, 41, 34, 4};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	int _ = 25; 
END
CASE(5)
	int road_[] = {1, 0};
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	int _ = 0; 
END
/*
CASE(6)
	int road_[] = ;
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	int _ = ; 
END
CASE(7)
	int road_[] = ;
	  vector <int> road(road_, road_+sizeof(road_)/sizeof(*road_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
