using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
class pair
{
public int first,second;
public pair(int a,int b)
{
first = a;
second = b;
}
}
static int N = 5010;
static int n, x;
static ArrayList g = new ArrayList();
static bool []vis = new bool[N];
static bool []good = new bool[N];
static int cnt;
static void Add_EDGE(int u, int v)
{
((ArrayList)g[u]).Add(v);
}
static void dfs1(int v)
{
good[v] = true;
for (int to = 0; to < ((ArrayList)g[v]).Count; to++)
if (!good[(int)((ArrayList)g[v])[to]])
dfs1((int)((ArrayList)g[v])[to]);
}
static void dfs2(int v)
{
vis[v] = true;
++cnt;
for (int to = 0; to < ((ArrayList)g[v]).Count; to++)
if (!vis[(int)((ArrayList)g[v])[to]] && !good[(int)((ArrayList)g[v])[to]])
dfs2((int)((ArrayList)g[v])[to]);
}
class sortHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
pair first = (pair)a;
pair second = (pair)b;
return first.first - second.first;
}
}
static int Minimum_Edges()
{
dfs1(x);
ArrayList val = new ArrayList();
for (int i = 0; i < n; ++i)
{
if (!good[i])
{
cnt = 0;
for(int j = 0; j < vis.Length; j++)
vis[j] = false;
dfs2(i);
val.Add(new pair(cnt, i));
}
}
val.Sort(new sortHelper());
int ans = 0;
for (int it = 0; it < val.Count; it++)
{
if (!good[((pair)val[it]).second])
{
++ans;
dfs1(((pair)val[it]).second);
}
}
return ans;
}
public static void Main(string []args)
{
n = 5; x = 4;
for(int i = 0; i < N + 1; i++)
g.Add(new ArrayList());
Add_EDGE(0, 1);
Add_EDGE(1, 2);
Add_EDGE(2, 3);
Add_EDGE(3, 0);
Console.WriteLine(Minimum_Edges());
}
}