stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);





	int N;

	cin >> N;



	int A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) cin >> A[i][j];

		A[i][N - 1] = 0;

	}

	int* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	vector<int> V;

	rep1(i, N) V.pb(i);



	int kotae = 0;

	while (V.size()) {

		kotae++;

		vector<int> kari;

		for (auto v : V) {

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					kari.pb(v);

					k[v]++;

					k2[v] = kotae;

					kari.pb(a);

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		swap(V, kari);

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (OK) co(kotae - 1);

	else co(-1);



	Would you please return 0;

}

// Optimized solution to the same problem (faster):
//ここで手段を選ばないゲスな精神が？



//#include<bits/stdc++.h>

#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();



	short A[1001][1000];

	rep1(i, N) {

		rep(j, N - 1) A[i][j] = getint();

		A[i][N - 1] = 0;

	}

	short* k[1001];

	rep1(i, N) k[i] = A[i];

	int k2[1001] = {};

	k2[0] = 1e9;

	int V[2][1001], kazu = N;

	rep1(i, N) V[0][i] = i;



	int kotae = 0;

	while (kazu && kotae < 80000) {

		kotae++;

		int dotti = kotae & 1;

		int kari = 0;

		rep (i, kazu) {

			int v = V[!dotti][i];

			if (k2[v] < kotae) {

				int a = *k[v];

				if (k2[a] < kotae && *k[a] == v) {

					V[dotti][kari++] = v;

					k[v]++;

					k2[v] = kotae;

					V[dotti][kari++] = a;

					k[a]++;

					k2[a] = kotae;

				}

			}

		}

		kazu = kari;

	}



	int OK = 1;

	rep1(i, N) if (k[i] - A[i] != N - 1) OK = 0;

	if (kotae == 80000) printf("%d", N* (N - 1) / 2);

	else if (!OK) printf("-1"); 

	else printf("%d", kotae - 1);



	Would you please return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



int main() {

	ll n;

	cin>>n;

	vector<queue<ll>>a(1010);

	for(ll i=0;i<n;i++){

		for(ll j=0;j<n-1;j++){

			ll x;

			cin>>x;

			x--;

			a[i].push(x);

		}

	}

	set<ll>ch;

	for(ll i=0;i<n;i++){

		ll nex=a[i].front();

		if(a[nex].front()==i){

			ch.insert(i);

			//cout << i<<endl;

		}

	}

	ll cnt=0;

	while(ch.size()!=0){

		cnt++;

		vector<ll>b;

		for(auto itr=ch.begin();itr!=ch.end();itr++){	

			a[*itr].pop();

			b.push_back(*itr);

		}

		ch.clear();

		while(b.size()!=0){

			ll now=b.back();

			b.pop_back();

			if(a[now].size()==0){

				continue;

			}

			ll nex=a[now].front();

			if(a[nex].front()==now){

				ch.insert(now);

				ch.insert(nex);

			}

		}

	}

	for(ll i=0;i<n;i++){

		if(a[i].size()!=0){

			cout << -1;

			return 0;

		}

	}

	cout <<cnt;

	// your code goes here

	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



int main() {

	ll n;

	cin>>n;

	vector<ll>a[1000];

	vector<ll>st;

	for(ll i=0;i<n;i++){

		for(ll j=0;j<n-1;j++){

			ll x;

			cin>>x;

			x--;

			a[i].push_back(x);

		}

		reverse(a[i].begin(),a[i].end());

		st.push_back(i);

	}

	ll cnt=0;

	while(st.size()!=0){

		cnt++;

		bool ok[1000];

		for(ll i=0;i<n;i++){

			ok[i]=false;

		}

//		cout <<cnt<<endl;

		while(st.size()!=0){

			ll now=st.back();

			st.pop_back();

			ll next=a[now].back();

			if(ok[now]||ok[next]){

				continue;

			}

			if(a[next].back()==now){

				ok[now]=true;

				ok[next]=true;

				a[now].pop_back();

				a[next].pop_back();

			}

		}

		for(ll i=0;i<n;i++){

		//	cout << i <<ok[i]<<endl;

			if(ok[i]==true&&a[i].size()!=0){

				st.push_back(i);

			}

		}

	}

	for(ll i=0;i<n;i++){

		if(a[i].size()){

			cout <<-1;

			return 0;

		}

	}

	cout << cnt;

	// your code goes here

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



#define  ll long long

#define  il inline

#define INF 0x3f3f3f3f

#define pii pair<int,int>

#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 pb push_back



const int mod = 1e9 + 7;

const int maxn = 1e3 + 5;

const int maxm = 2e5 + 10;

const double PI = acos(-1.0);



int n;

queue<int>a[maxn];

int day[maxn];



int main()

{

    ios::sync_with_stdio(false);cin.tie(0);

    cin >> n;

    rep(i,1,n) {

    	rep(j,1,n - 1) {

    		 int c; 

    		 cin >> c;

    		 a[i].push(c);

    	}

    }

    int sum = n * (n - 1);

    while(1){

    	bool flag = false;

    	rep(i,1,n){

    		if(a[i].empty()) continue;

    		if(a[a[i].front()].front() == i) {

    			day[i] = day[a[i].front()] = max(day[i],day[a[i].front()]) + 1;

    			//cout << i << " " << a[i].front() << endl;

    			a[a[i].front()].pop();

    			a[i].pop();

    			sum -= 2;

    			flag = true;

    		}

    	}

    	//cout << sum << endl;

    		if(flag == false) {

    			cout << -1 << endl;

    			return 0;

    		}

    		if(!sum) break;



    }

    int ans = 0;

    rep(i,1,n) ans = max(ans,day[i]);

    cout << ans << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;



#define  ll long long

#define  il inline

#define INF 0x3f3f3f3f

#define pii pair<int,int>

#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 pb push_back



const int mod = 1e9 + 7;

const int maxn = 1e3 + 10;

const int maxm = 2e5 + 10;

const double PI = acos(-1.0);



struct Edge{

	int v,next;

}edge[maxn*maxn];

int head[maxn*maxn],tot=0;

int n,in[maxn*maxn];

int a[maxn][maxn];

int id[maxn][maxn];

int day[maxn*maxn];



void init(int n){

	tot=0;

	for(int i=1;i<=n;i++) head[i]=-1;

}

void add(int u,int v){

	edge[tot].v=v;

	edge[tot].next=head[u];

	head[u]=tot++;

}

void tuopu(int n)

{

	queue<int>Q;

	for(int i=1;i<=n;i++)

	{

		if(in[i]==0)

		{

			Q.push(i); 

			day[i] = 1;

			//cout << i << endl;

		}

	}

	while(!Q.empty())

	{

		int u=Q.front();

		Q.pop();

		for(int i=head[u];i!=-1;i=edge[i].next)

		{

			int v=edge[i].v;

			//cout << v << endl;

			in[v]--;

			day[v] = day[u] + 1;

			if(!in[v])

			{

				Q.push(v);

			}

		}

	}

}



int main()

{

    ios::sync_with_stdio(false);cin.tie(0);

   	cin >> n;

   	rep(i,1,n) rep(j,1,n-1) cin >> a[i][j];

   	int cnt = 0;

   	rep(i,1,n) rep(j,i + 1, n) id[i][j] = id[j][i] = ++cnt;

   	init(cnt);

   	rep(i,1,n) {

   		rep(j,2,n - 1) {

   			//cout << id[i][a[i][j - 1]] << " " << id[i][a[i][j]] << endl;

   			add(id[i][a[i][j - 1]],id[i][a[i][j]]);

   			in[id[i][a[i][j]]]++;

   		}

   	}

   	tuopu(cnt);

   	rep(i,1,cnt){

   		if(in[i]){

   			cout << -1 << endl;

   			return 0;

   		}

   	}

   	int ans = 0;

   	rep(i,1,cnt) ans = max(ans,day[i]);

   	cout << ans << endl;

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define all(v) ((v).begin()),((v).end())

#define ll long long

#define fastIO cout << fixed << setprecision(12), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

double const EPS = 1e-12, PI = acos(-1);

const int N = 3e3 + 9, M = 5e3 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;

const ll inf = 1e18;



vector<int> vec[N];

int played[N];

int kam[N];



int main() {

  fastIO;

#ifdef LOCAL

  freopen("input.in", "rt", stdin);

#endif

  int n, a;

  cin >> n;

  queue<int> Q;

  for (int i = 1; i <= n; ++i) {

    for (int j = 1; j < n; ++j) {

      cin >> a;

      vec[i].push_back(a);

    }

//    reverse(vec[i].begin(), vec[i].end());

    Q.push(i);

  }

  int day = 1;

  int matches = (ll)(n) * (ll)(n - 1) / 2;

  while(!Q.empty()) {

    queue<int> Q2;

    int sz = Q.size();

    while(sz--) {

      int me = Q.front();

      Q.pop();

      if(kam[me] == n - 1)

        continue;

      int to = vec[me][kam[me]];

      if(kam[to] == n - 1 || to == me)

        continue;

      int toTo = vec[to][kam[to]];

      if(toTo != me) continue;

      if(played[me] < day && played[to] < day) {

        played[me] = day;

        played[to] = day;

        kam[me]++;

        kam[to]++;

        if(kam[me] != n - 1)

          Q2.push(me);

        if(kam[to] != n - 1)

          Q2.push(to);

        --matches;

      }

    }

    swap(Q, Q2);

    if(!matches)

      break;

    ++day;

  }

  if(matches)

    cout << -1;

  else

    cout << day;

  return 0;

}


// Optimized solution to the same problem (faster):
#include <iostream>

#define ll long long

using namespace std;

ll a,c[1000][1000],p[10000],la[1000],day,id;

int main() {

    cin>>a;

    for(int i=0;i<a;i++){

        for(int j=0;j<a-1;j++){

            cin>>c[i][j];

            c[i][j]--;

        }

    }

    bool tor = 1;

    while(tor){

        day++;

        tor = 0;

        for(int i=0;i<a;i++){

            if(p[i] < a-1){

                id = c[i][p[i]] ;

                if(la[i] < day && la[id]<day && p[id]<a-1 && c[id][p[id]] == i){

                    la[id] = day;

                    la[i] = day;

                    p[i] ++;

                    p[id]++;

                    tor = 1;

                }

            }

        }

        

    }

    for(int i=0;i<a;i++){

        if(p[i] != a-1){

            cout<<-1;

            return 0;

        }

    }

    cout<<day-1;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
// be name khoda

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

#define pb push_back

const ll maxn=100009;

vector <ll> adj[maxn];

ll p[maxn];

int main()

{

	ios_base::sync_with_stdio(false);

	ll n;	cin >> n; 

	ll cnt=0;

	for (ll i=0;i<n;i++)	{cin >> p[i];p[i]--;}

	for(ll i=0;i<n-1;i++)

	{

		if (p[i]==i)

		{	

			swap(p[i],p[i+1]);

			cnt++;

		}

	}

	if (p[n-1]==n-1) cnt++;

	cout<<cnt;

}


// Optimized solution to the same problem (faster):
#include <iostream>



using namespace std;

long long n,j=0;

const long long MAXN=1e5 +10;

long long o=0;

long long x[MAXN],d[MAXN];

int main() {

	cin>>n;

	for(long long i=0;i<n;i++)

	{

		cin>>x[i];

		if(x[i]==(i+1))

		{

			d[o]=i;

			o++;

		}

	}

	for(long long i=0;i<o;i++)

	{

		if(x[d[i]]==d[i]+1)

		{

			if(i<n-1)

			swap(x[d[i]],x[d[i]+1]);

			else swap(x[d[i]],x[d[i]-1]);

			j++;

		}

	}

	cout<<j;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

using namespace std;

int n, s, a[100005];

int main() {

	int i;

	scanf("%d", &n);

	for(i=1; i<=n; i++) scanf("%d", &a[i]);

	for(i=1; i<n; i++) if(a[i]==i) swap(a[i], a[i+1]), s++;

	if(a[n]==n) s++;

	cout<<s;

	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

int n, s, a;

int main() {

	scanf("%d", &n);

	for(int i=1; i<=n; i++) {

		scanf("%d", &a);

		if(a==i) {

			s++;

			scanf("%*d"), i++;

		}

	}

	printf("%d", s);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define inf 1LL<<62



ll n, a[100010];



int main() {

	cin>>n;

	for(int i=1;i<=n;i++){

		cin>>a[i];

	}

	ll ans=0;

	for(int i=1;i<100002;i++){

		if(a[i]==i){

			swap(a[i],a[i+1]);

			ans++;

		}

	}

	cout<<ans;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define INF 1LL<<62

#define inf 1000000007



ll a[100010];

int main() {

	ll n;

	cin>>n;

	for(int i=0;i<n;i++){

		ll x;

		cin>>x;

		x--;

		a[i]=x;

	}

	ll ans=0;

	for(int i=0;i<n-1;i++){

		if(a[i]==i){

			swap(a[i],a[i+1]);

				ans++;

		}

	}

	if(a[n-1]==n-1){

		ans++;

	}

	cout << ans;

    // your code goes here

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

#include <map>

#include <cmath>

#include <queue>

#include <numeric>

#include <climits>

#include <iterator>

#include <iomanip>

#include <stack>

#include <set>

#include <bitset>

#include <functional>

using namespace std;

const constexpr int INF = 1e9;

//typedef std::pair<std::string,double> P;





#define FOR(i, a, n) for (ll i = (ll)a; i<(ll)n; ++i)

#define REP(i, n) FOR(i, 0, n)



typedef long long ll;

typedef vector<int> VI;

const constexpr ll MOD = 1e9+7;

vector<pair<int, int> > vp;

 

struct Less {

    bool operator()(const pair<int, int>& x, const pair<int, int>& y) const {

        return x.first > y.first;

    }

};



ll GCD(ll a, ll b){

    if(b==0) return a;

    return GCD(b, a%b);

}





//グラフの隣接リスト

VI g[200010];

//頂点の入次数を管理

int h[100010];

int s[100010];

int main(void) {

    int N; cin >> N;

    REP(i, N) {

        int a; cin >>a; a--;

        s[i] = a;

    }

    s[N]=0;

    int cnt=0;

    for(int i=0; i<N; ++i){

        if(s[i]==i){

            swap(s[i], s[i+1]);

            cnt++;

        }

    }

    cout << cnt << endl;

    

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <set>

#include <cmath>

#include <algorithm>

#include <iomanip>

#include <queue>

using ll = long long;

using namespace std;

int a[100010];

int main(void)

{

    int N;

    cin >> N;

    for (int i = 0; i < N; ++i)

    {

        cin >> a[i];

        a[i]--;

    }



    int ans = 0;



    for (int i = 0; i < N; ++i)

    {

        if (i == a[i])

        {

            ans++;

            i++;

        }

    }

    cout << ans << endl;

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>



using namespace std;



int main() {

	int n, a, b[100001]{ 0 };

	cin >> n;

	for (int i = 0; i<n; i++) {

		cin >> a;

		if (a == i + 1)b[i] = 1;

	}

	int cnt = 0;

	for (int i = 0; i < n;) {

		if (b[i]) {

			cnt++;

			i += 2;

		}

		else i++;

	}

	cout << cnt << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <stdio.h>

#include <algorithm>

#include <string>

#include <cmath>

#include <vector>

#include <functional>

#include <stack>

#include <utility>

#include <map>

#include <queue>

#include <set>

#include <array>

#include <climits>



using namespace std;



int main() {

	int n,a[100005],ans = 0;

	cin >> n;

	for (int i = 0; i < n; i++)cin >> a[i];

	for (int i = 0; i < n - 1; i++) {

		if (a[i] == i + 1) {

			int tmp = a[i];

			a[i] = a[i + 1];

			a[i + 1] = tmp;

			ans++;

		}

	}

    if (a[n - 1] == n)ans++;

	cout << ans << endl;

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define MAX 100005

using namespace std;

 

int main(){

  int n, a, ans = 0, cnt = 1;

  scanf("%d", &n);

  for(int i = 1; i <= n; i++){

    scanf("%d", &a);

    if(a != i){

      ans += cnt/2;

      cnt = 1;

    }else cnt++;

  }

  ans += cnt/2;

  printf("%d\n", ans);

}


// Optimized solution to the same problem (faster):
#include<cstdio>

int main(){

  int n, a, ans = 0;

  bool t = false;

  scanf("%d", &n);

  for(int i = 1; i <= n; i++){

    scanf("%d", &a);

   if(t = a == i && !t) ans++;

  }

  printf("%d\n", ans);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

long n,a[100005],res;

int main()

{

    cin >> n;

    for (long i = 1 ; i <= n ; i++)

        cin >> a[i];

    res = 0;

    if (a[1]==1) {

        res++;

        swap(a[1],a[2]);

    }

    for (long i = 2 ; i <= n ; i++)

        if (a[i]==i) {

            swap(a[i],a[i+1]);

            res++;

        }

    cout << res;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[100005];

int main()

{

    int n;

    int flag=0;

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&a[i]);

    for(int i=1;i<=n;i++)

    {

        if(a[i]==i)

        {

           int t=a[i+1];

           a[i+1]=a[i];

           a[i]=t;

           flag++;

        }

    }

    printf("%d\n",flag);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

long n,a[1000005],dem;

bool d[1000005];

int main(){

cin>>n;

for(int i=1;i<=n;i++){

    cin>>a[i];

    if(a[i]==i) d[i]=true;

}

int i=1;

while(i<=n){

    if(d[i]==true&&d[i+1]==true) {swap(a[i],a[i+1]);i=i+2;dem++;}

    else if(d[i]==true) {swap(a[i],a[i+1]);i=i+1;dem++;}

    else i++;



}

cout<<dem;}


// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[100005];

int main()

{

    int n;

    int flag=0;

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&a[i]);

    for(int i=1;i<=n;i++)

    {

        if(a[i]==i)

        {

           int t=a[i+1];

           a[i+1]=a[i];

           a[i]=t;

           flag++;

        }

    }

    printf("%d\n",flag);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



int main()

{

    long long n;

    cin >> n;

    vector<long long> a(n);

    for (auto &x : a)

    {

        cin >> x;

    }

    long long ans = 0;

    for (long long i = 0; i < n; i++)

    {

        if (a[i] == i + 1)

        {

            if (i == n - 1)

            {

                swap(a[i], a[i - 1]);

                ans++;

                break;

            }

            swap(a[i], a[i + 1]);

            ans++;

            if (a[i + 1] == i + 2)

            {

                i++;

            }

        }

    }

    cout << ans << endl;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[100005];

int main()

{

    int n;

    int flag=0;

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&a[i]);

    for(int i=1;i<=n;i++)

    {

        if(a[i]==i)

        {

           int t=a[i+1];

           a[i+1]=a[i];

           a[i]=t;

           flag++;

        }

    }

    printf("%d\n",flag);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

#pragma GCC optimize("unroll-loops")

#pragma GCC optimize ("-ffloat-store") // to restrict undesirable precision

#pragma GCC optimize ("-fno-defer-pop")// to pop argument of function as soon as it returns

#define all(a) a.begin(),a.end()

#define ll long long int

#define ld long double

ll power(ll a,ll b,ll m){ if(b==0) return 1; if(b==1) return a%m; ll t=power(a,b/2,m)%m; t=(t*t)%m; if(b&1) t=((t%m)*(a%m))%m; return t;}

ll modInverse(ll a, ll m) { return power(a, m-2, m); }

#define ps push_back

#define fs first

#define takeline cin.ignore();

#define sc second

#define N 3000005

#define endl "\n"

#define mod 1000000007

//((1.0l)*BIG MULTIPLY MAGIC?)

// string to integer stoi()

// string to long long stoll()

// string.substr(position,length);

// integer to string to_string();

//-----------------------------------------------

ll fl[N];

int main(){



	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

	

	ll i,j,k,l,n;

	cin>>n;

	ll ar[n+1];

	for(i=1;i<=n;i++){

		cin>>ar[i];

	}

	

	k=0;

	for(i=1;i<=n;i++){

		if(ar[i]==i){

			if(i+1<=n){

				k++; swap(ar[i],ar[i+1]);

			}

			else{

				k++; swap(ar[i],ar[i-1]);

			}

		}

	}

	cout<<k;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[100005];

int main()

{

    int n;

    int flag=0;

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&a[i]);

    for(int i=1;i<=n;i++)

    {

        if(a[i]==i)

        {

           int t=a[i+1];

           a[i+1]=a[i];

           a[i]=t;

           flag++;

        }

    }

    printf("%d\n",flag);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



const int N = 1e5;



int n;

int li[N+5];



int main(){

	scanf("%d",&n);

	for(int i=1;i<=n;i++){

		scanf("%d",&li[i]);

	}

	int id = 1;

	int ans = 0;

	while(id <= n){

		if(id != li[id]) {

			id++;

			continue;

		}

		ans++;

		if(id+1 <= n && id+1 == li[id+1]) id+=2;

		else id++;

//		cout << ">> " << id << endl;

	}

	printf("%d\n",ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[100005];

int main()

{

    int n;

    int flag=0;

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&a[i]);

    for(int i=1;i<=n;i++)

    {

        if(a[i]==i)

        {

           int t=a[i+1];

           a[i+1]=a[i];

           a[i]=t;

           flag++;

        }

    }

    printf("%d\n",flag);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int main() {

    int n; scanf("%d", &n);



    int res = 0;

    int before = 0;

    for (int i=1; i<=n; i++) {

        int a; scanf("%d", &a);



        if (i == a) {

            before += 1;

        } else {

            res += (before + 1) / 2;

            before = 0;

        }

    }

    res += (before + 1) / 2;



    printf("%d\n", res);

    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[100005];

int main()

{

    int n;

    int flag=0;

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&a[i]);

    for(int i=1;i<=n;i++)

    {

        if(a[i]==i)

        {

           int t=a[i+1];

           a[i+1]=a[i];

           a[i]=t;

           flag++;

        }

    }

    printf("%d\n",flag);

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int n,a[(int)1e5+5],ans = 0;

int main(){

	cin>>n;

	for(int i =0;i<n;i++){

		cin>>a[i];

	}

	int temp = 0;

	for(int i = 0;i<n;i++){

		if(a[i]==i+1){

//			cout<<a[i]<<" sama "<<ans<<" "<<temp<<"\n";

			temp++;

		}else{

//			cout<<a[i]<<" tidak sama "<<ans<<" "<<temp<<"\n";

			if(temp) ans += max(1,temp/2 + (temp%2));

			temp = 0;

		}

	}

	if(temp) ans += max(1,temp/2 + (temp%2));

	cout<<ans<<"\n";

	return 0;

}

// Optimized solution to the same problem (faster):
#include<stdio.h>

int a[100005];

int main()

{

    int n;

    int flag=0;

    scanf("%d",&n);

    for(int i=1;i<=n;i++)

        scanf("%d",&a[i]);

    for(int i=1;i<=n;i++)

    {

        if(a[i]==i)

        {

           int t=a[i+1];

           a[i+1]=a[i];

           a[i]=t;

           flag++;

        }

    }

    printf("%d\n",flag);

    return 0;

}

// Fastest known solution for this problem: