#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 300010;

int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);

	ll N;
	cin >> N;
	ll res = N;

	for (int b = 0;1ll << b <= N;++b) {
		ll c = N % (1ll<<b);
		ll a = N / (1ll<<b);
		chmin(res, a + b + c);
	}
	cout << res << '\n';
}


//================code===================//

//#define TLE

#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>


#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long

#define ld double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<LL,LL>
#define micro 0.000001

using namespace std;

LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }


#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#else
#define AT
#define AE
#define ce(t)
#endif

pair <int, int> vu[9] = { {0,-1},{1,0},{0,1} ,{-1,0},{0,0},{1,1}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN

template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };

const int max_v = 2e5+ 7;
const int max_k = 1e1 + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = 998244353;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; }

bool open[max_v],vist[max_v];
vector<int> adj[max_v];
int sn = 0;

void dfs(int u)
{	
	open[u] = 1;

	for (auto& v : adj[u]) if (!vist[v])
	{
		if (open[v])
		{
			++sn;
			open[0] = 0;
			return;
		}

		dfs(v);
	}

	open[u] = 0;
}

void dfs2(int u)
{
	vist[u] = 1;
	for (auto& v : adj[u]) if (!vist[v])
	{
		dfs2(v);
	}
}

int main()
{
#ifdef OHSOLUTION
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	int n; ci(n);

	fa(i, 0, n)
	{
		int x; ci(x), --x;
		adj[i].push_back(x);
	}

	fa(i, 0, n) if (!vist[i]) dfs(i),dfs2(i);

	LL base = 2;
	LL ret = 1;

	while (sn)
	{
		if (sn & 1) ret = ret * base % mod;
		base = base * base % mod;
		sn >>= 1;
	}

	if (ret) --ret;
	else ret = mod - 1;

	co(ret << "\n");

	return 0;
}
