stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<iostream>

#include<set>

using namespace std;

struct A{

	int is = 0;

	int num;

}pp[500000];

int main(){

	pp[1].is = 1;

	int n, i, y = 0;

	cin >> n;

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

		cin >> pp[i].num;

	}

	int Count = 0, t = 1;

	while(pp[2].is!=1){

		pp[t].is = 1;

		t = pp[t].num;

		Count++;

		if(Count>200000){

			cout << -1 << endl;

			y = 1;

			break;

		}

	} 

	if(!y){

		cout << Count-1 << endl;

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 5.225039826803241x):
#include<iostream>

using namespace std;



int a[100000];

int main()

{

	int N;

	int i = 1;

	int j =0;

	int ini = 1;

	int flag = 0;

	cin>>N;

	int k = N;



	while(k--)

	{

		cin>>a[i++]; 

	}



	do{

		j++;

		if(a[ini]==2)

		{

			flag=1;

		}

		else

			ini = a[ini];

	

	}while(j<=N&&flag==0);

	if(flag==0)

		cout<<-1<<endl;

	else

		cout<<j<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<set>

using namespace std;

struct A{

	int is = 0;

	int num;

}pp[500000];

int main(){

	pp[1].is = 1;

	int n, i, y = 0;

	cin >> n;

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

		cin >> pp[i].num;

	}

	int Count = 0, t = 1;

	while(pp[2].is!=1){

		pp[t].is = 1;

		t = pp[t].num;

		Count++;

		if(Count>200000){

			cout << -1 << endl;

			y = 1;

			break;

		}

	} 

	if(!y){

		cout << Count-1 << endl;

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 27.623120999226863x):
#include<stdio.h>

int a[100003];

int main()

{

	long long b,c,d;

	scanf("%lld",&b);

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

	{

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

	} 

	c=a[1];

	if(c==2) printf("1");

	else {

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

	{

	d=a[c];

	if(d==2) 

	{

	printf("%d",i+1);

	break;

   }

	c=d;

	if(i==b-1){

     

	printf("-1");

	break;

	}       

}

}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cmath>

#include <vector>

#include <string>

#include <iomanip>

//#include <string.h>

#include <bits/stdc++.h>

#define INF 999999999

using namespace std;

#define ll long long

#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int vis[1000000]={0};

int hh[1000000];

int main()

{

    boost;

    int n;

    cin >>n;

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

        cin >>hh[i];

    }

    int j=1,sum=0,falg=0,p=1;

    vis[1]=1;

    while(1){

        if(vis[2]==1)

            break;

        else if(vis[j]==2){

            cout<<-1<<endl;

            falg=1;

            break;

        }

        else {

            j=hh[j];

            //cout<<j<<endl;

            vis[j]++;

            //cout<<" "<<vis[j]<<endl;

            sum++;

        }

    }

    if(!falg)

        cout<<sum<<endl;

    return 0;



}


// Faster C++ solution for the same problem (measured speedup: 5.35270522550678x):
#include<stdio.h>

int a[100003];

int main()

{

	long long b,c,d;

	scanf("%lld",&b);

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

	{

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

	} 

	c=a[1];

	if(c==2) printf("1");

	else {

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

	{

	d=a[c];

	if(d==2) 

	{

	printf("%d",i+1);

	break;

   }

	c=d;

	if(i==b-1){

     

	printf("-1");

	break;

	}       

}

}

	return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;



int a[100000];

int main()

{

	int N;

	int i = 1;

	int j =0;

	int ini = 1;

	int flag = 0;

	cin>>N;

	int k = N;



	while(k--)

	{

		cin>>a[i++]; 

	}



	do{

		j++;

		if(a[ini]==2)

		{

			flag=1;

		}

		else

			ini = a[ini];

	

	}while(j<=N&&flag==0);

	if(flag==0)

		cout<<-1<<endl;

	else

		cout<<j<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.286681425379126x):
#include<stdio.h>

int a[100003];

int main()

{

	long long b,c,d;

	scanf("%lld",&b);

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

	{

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

	} 

	c=a[1];

	if(c==2) printf("1");

	else {

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

	{

	d=a[c];

	if(d==2) 

	{

	printf("%d",i+1);

	break;

   }

	c=d;

	if(i==b-1){

     

	printf("-1");

	break;

	}       

}

}

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<set>

using namespace std;

struct A{

	int is = 0;

	int num;

}pp[500000];

int main(){

	pp[1].is = 1;

	int n, i, y = 0;

	cin >> n;

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

		cin >> pp[i].num;

	}

	int Count = 0, t = 1;

	while(pp[2].is!=1){

		pp[t].is = 1;

		t = pp[t].num;

		Count++;

		if(Count>200000){

			cout << -1 << endl;

			y = 1;

			break;

		}

	} 

	if(!y){

		cout << Count-1 << endl;

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 26.55158373144367x):
#include<stdio.h>

int main()

{

	int N;

	scanf("%d",&N);

	int time=0;

	int a[100000];

	int i;

	int x=0;

	for(i=0;i<=N-1;i++)

	{

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

		if(a[i]==2)

		{

			x=1;

		}

	}

	if(x==1)

	{

		int t=0;

		while(t!=1)

		{

			t=a[t]-1;

			time+=1;

			if(time>N-1)

			{

				printf("-1");

				return 0;

			}

		}

		printf("%d",time);

	}

	else

	{

		printf("-1");		

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cmath>

#include <vector>

#include <string>

#include <iomanip>

//#include <string.h>

#include <bits/stdc++.h>

#define INF 999999999

using namespace std;

#define ll long long

#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int vis[1000000]={0};

int hh[1000000];

int main()

{

    boost;

    int n;

    cin >>n;

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

        cin >>hh[i];

    }

    int j=1,sum=0,falg=0,p=1;

    vis[1]=1;

    while(1){

        if(vis[2]==1)

            break;

        else if(vis[j]==2){

            cout<<-1<<endl;

            falg=1;

            break;

        }

        else {

            j=hh[j];

            //cout<<j<<endl;

            vis[j]++;

            //cout<<" "<<vis[j]<<endl;

            sum++;

        }

    }

    if(!falg)

        cout<<sum<<endl;

    return 0;



}


// Faster C++ solution for the same problem (measured speedup: 5.145066735534959x):
#include<stdio.h>

int main()

{

	int N;

	scanf("%d",&N);

	int time=0;

	int a[100000];

	int i;

	int x=0;

	for(i=0;i<=N-1;i++)

	{

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

		if(a[i]==2)

		{

			x=1;

		}

	}

	if(x==1)

	{

		int t=0;

		while(t!=1)

		{

			t=a[t]-1;

			time+=1;

			if(time>N-1)

			{

				printf("-1");

				return 0;

			}

		}

		printf("%d",time);

	}

	else

	{

		printf("-1");		

	}

	return 0;

}
// Slower C++ solution:
#include<iostream>

using namespace std;



int a[100000];

int main()

{

	int N;

	int i = 1;

	int j =0;

	int ini = 1;

	int flag = 0;

	cin>>N;

	int k = N;



	while(k--)

	{

		cin>>a[i++]; 

	}



	do{

		j++;

		if(a[ini]==2)

		{

			flag=1;

		}

		else

			ini = a[ini];

	

	}while(j<=N&&flag==0);

	if(flag==0)

		cout<<-1<<endl;

	else

		cout<<j<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.081604085626336x):
#include<stdio.h>

int main()

{

	int N;

	scanf("%d",&N);

	int time=0;

	int a[100000];

	int i;

	int x=0;

	for(i=0;i<=N-1;i++)

	{

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

		if(a[i]==2)

		{

			x=1;

		}

	}

	if(x==1)

	{

		int t=0;

		while(t!=1)

		{

			t=a[t]-1;

			time+=1;

			if(time>N-1)

			{

				printf("-1");

				return 0;

			}

		}

		printf("%d",time);

	}

	else

	{

		printf("-1");		

	}

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<set>

using namespace std;

struct A{

	int is = 0;

	int num;

}pp[500000];

int main(){

	pp[1].is = 1;

	int n, i, y = 0;

	cin >> n;

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

		cin >> pp[i].num;

	}

	int Count = 0, t = 1;

	while(pp[2].is!=1){

		pp[t].is = 1;

		t = pp[t].num;

		Count++;

		if(Count>200000){

			cout << -1 << endl;

			y = 1;

			break;

		}

	} 

	if(!y){

		cout << Count-1 << endl;

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 5.173522343487967x):
#include <bits/stdc++.h>

using namespace std;

const int N=1e5+5;

#define inf 0x3f3f3f3f

#define ll long long

int a[N];

int main() 

{

	int n;

	cin>>n;

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

		cin>>a[i];

	int ans=0;

	int i=1;

	map<int,int> mp;

	int flag=0;

	while(1)

	{

		i=a[i];

		ans++;

		if(i==2)

			break;

		if(mp[i])

		{

			flag=1;

			break;

		}

		mp[i]=1;

	}

	if(flag)

		cout<<-1<<endl;

	else 

		cout<<ans<<endl;

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<set>

using namespace std;

struct A{

	int is = 0;

	int num;

}pp[500000];

int main(){

	pp[1].is = 1;

	int n, i, y = 0;

	cin >> n;

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

		cin >> pp[i].num;

	}

	int Count = 0, t = 1;

	while(pp[2].is!=1){

		pp[t].is = 1;

		t = pp[t].num;

		Count++;

		if(Count>200000){

			cout << -1 << endl;

			y = 1;

			break;

		}

	} 

	if(!y){

		cout << Count-1 << endl;

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 5.171227932839843x):
#include <iostream>

#include <map>

#include <vector>

#define ll long long

using namespace std;



int main(){

    ll n, number;

    cin >> n;

    map<ll, ll > way;

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

        cin >> number;

        way.insert(make_pair(i, number));

    }

    ll ct = 0, num = 1;

    while(ct <= n) {

        num = way[num];

        ct++;

        if(num == 2) break;

    }

    if(num == 2) cout << ct << endl;

    else cout << -1 << endl;

    return 0;

}
// Slower C++ solution:
#include<iostream>

#include<set>

using namespace std;

struct A{

	int is = 0;

	int num;

}pp[500000];

int main(){

	pp[1].is = 1;

	int n, i, y = 0;

	cin >> n;

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

		cin >> pp[i].num;

	}

	int Count = 0, t = 1;

	while(pp[2].is!=1){

		pp[t].is = 1;

		t = pp[t].num;

		Count++;

		if(Count>200000){

			cout << -1 << endl;

			y = 1;

			break;

		}

	} 

	if(!y){

		cout << Count-1 << endl;

	}

	return 0;

} 

// Faster C++ solution for the same problem (measured speedup: 5.227288843581109x):
#include <bits/stdc++.h>

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

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

#define cinf(x,n) for(ll i=0;i<(n);i++)cin>>x[i];

#define coutf(x,n) for(ll i=0;i<(n);i++)cout<<x[i]<<endl;

typedef long long ll;

typedef unsigned long long ull;

typedef long double ld;

 

using namespace std;

 

ll cnt;

 

int main(){

	ll n;

	cin >> n;

	ll a[n];

	cinf(a,n);

	ll start=1;

	while(start!=2){

		start=a[start-1];

		cnt++;

		if(cnt>n){

			cout << -1 << endl;

			return 0;

		}

	}

	cout << cnt << endl;

}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>



#define ll long long

using namespace std;



ll N;

vector<ll> a;





int main(){

  ll inp;

  ll num=1;

  ll result=0;

  

  cin >> N;

  int checked[100000]={0};

  

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

    cin >> inp;

    a.push_back(inp);

  }

  

  do{

    result++;

    if(a[num-1]==2){

      cout << result << endl;

      return 0;

    }

    else{

      checked[num-1]=1;

      num=a[num-1];

    }

  }while(!checked[num-1]);



  cout << "-1" << endl;

  return 0;

  

}


// Faster C++ solution for the same problem (measured speedup: 1.3791922949855058x):
#include <iostream>

#include <iomanip>

#include <vector>

#include <algorithm>

#include <cstring>

#include <map>

#include <queue>

#include <cmath>

#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3

using namespace std;



#define MOD 1000000007

#define ll long long

#define ld long double

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

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

#define pb push_back

#define mp make_pair

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

#define rall(a) a.rbegin(),a.rend()

#define rmsame(a) sort(all(a)),a.erase(unique(all(a)), a.end())

#define rmvector(a,b) rep(i,a.size())rep(j,b.size())if(a[i]==b[j]){a.erase(a.begin()+i);i--;break;}

template<typename X> bool exist(vector<X> vec, X item){return find(all(vec), item)!=vec.end();}

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b,a%b);}

ll lcm(ll a, ll b){return a/gcd(a,b)*b;}







bool check[100005];

int main(){

  cin.tie(0);

  ios::sync_with_stdio(false);



  ll N; cin >> N ;



  vector<ll> A; 

  rep(i,N){

    ll inp; cin >> inp;

    A.pb(inp);

  }



  ll push = 1;

  rep(i,N){

    push--;

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

    push = A[push];

    if(check[push]) break;

    check[push] = true;

  }



  cout << "-1" << endl;



  

  

  //cout << fixed << setprecision(16) << ans << endl;

  

  return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

const int MAXN = 100000;

const int MAXK = 1000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[10 * MAXK + 5];

int main() 

{

	int N,K,Max=0,cnt = 0;

	scanf("%d%d",&N,&K);

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

	{

		char ch;

		scanf("%d%d %c",&x[i],&y[i],&ch);

		x[i]= x[i] % (2*K);

		y[i]= y[i] % (2*K);

		if(ch=='W' ) c[i] = 1;

		else c[i]=0;

	}

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

		cnt = 0;

		memset(y1,0,sizeof(y1));

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

			bool p = ( i <= x[j] && x[j] < i+K );

			bool q = ( 0 <= y[j] && y[j] < K );

			if( p ^ q == c[j] )

			{

				cnt++;

				y1[y[j]] ++;

			}

			else 

			{

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) 

		{

			if(cnt>Max) 

				Max = cnt;

			if(N-cnt>Max ) 

				Max=N-cnt;

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}


// Faster C++ solution for the same problem (measured speedup: 2.224462869906131x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int MAXN = 100000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[MAXN],N,K,Max,cnt;

int main() 

{



	scanf("%d%d",&N,&K);

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

	{

		char ch;

		scanf("%d %d %c",&x[i],&y[i],&ch);

		x[i]%=(2*K);

		y[i]%=(2*K);

		if(ch=='W' ) c[i] = 1;

	}

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

		cnt = 0;

		fill(y1,y1+2*K+1,0);

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

			bool p = ( i <= x[j] && x[j] < i+K );

			bool q = ( 0 <= y[j] && y[j] < K );

			if( p ^ q == c[j] )

			{

				cnt++;

				y1[y[j]] ++;

			}

			else 

			{

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) 

		{

			Max=std::max(Max,std::max(cnt,N-cnt));

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<cstring>

const int MAXN = 100000;

const int MAXK = 1000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[10 * MAXK + 5];

int main() 

{

	int N,K,Max=0,cnt = 0;

	scanf("%d%d",&N,&K);

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

	{

		char ch;

		scanf("%d%d %c",&x[i],&y[i],&ch);

		x[i]= x[i] % (2*K);

		y[i]= y[i] % (2*K);

		if(ch=='W' ) c[i] = 1;

		else c[i]=0;

	}

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

		cnt = 0;

		memset(y1,0,sizeof(y1));

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

			bool p = ( i <= x[j] && x[j] < i+K );

			bool q = ( 0 <= y[j] && y[j] < K );

			if( p ^ q == c[j] )

			{

				cnt++;

				y1[y[j]] ++;

			}

			else 

			{

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) 

		{

			Max=std::max(Max,std::max(cnt,N-cnt));

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}


// Faster C++ solution for the same problem (measured speedup: 2.224462869906131x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int MAXN = 100000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[MAXN],N,K,Max,cnt;

int main() 

{



	scanf("%d%d",&N,&K);

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

	{

		char ch;

		scanf("%d %d %c",&x[i],&y[i],&ch);

		x[i]%=(2*K);

		y[i]%=(2*K);

		if(ch=='W' ) c[i] = 1;

	}

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

		cnt = 0;

		fill(y1,y1+2*K+1,0);

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

			bool p = ( i <= x[j] && x[j] < i+K );

			bool q = ( 0 <= y[j] && y[j] < K );

			if( p ^ q == c[j] )

			{

				cnt++;

				y1[y[j]] ++;

			}

			else 

			{

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) 

		{

			Max=std::max(Max,std::max(cnt,N-cnt));

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

const int MAXN = 100000;

const int MAXK = 1000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[10 * MAXK + 5];

int main() 

{

	int N,K,Max=0,cnt = 0;

	scanf("%d%d",&N,&K);

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

	{

		char ch;

		scanf("%d%d %c",&x[i],&y[i],&ch);

		x[i]= x[i] % (2*K);

		y[i]= y[i] % (2*K);

		if(ch=='W' ) c[i] = 1;

		else c[i]=0;

	}

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

		cnt = 0;

		memset(y1,0,sizeof(y1));

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

			bool p = ( i <= x[j] && x[j] < i+K );

			bool q = ( 0 <= y[j] && y[j] < K );

			if( p ^ q == c[j] )

			{

				cnt++;

				y1[y[j]] ++;

			}

			else 

			{

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) 

		{

			if(cnt>Max) 

				Max = cnt;

			if(N-cnt>Max ) 

				Max=N-cnt;

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}


// Faster C++ solution for the same problem (measured speedup: 2.224462869906131x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int MAXN = 100000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[MAXN],N,K,Max,cnt;

int main() 

{

	scanf("%d%d",&N,&K);

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

	{

		char ch;

		scanf("%d %d %c",&x[i],&y[i],&ch);

		x[i]%=(2*K);

		y[i]%=(2*K);

		if(ch=='W' ) c[i] = 1;

	}

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

		cnt = 0;

		fill(y1,y1+2*K+1,0);

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

			bool p = (i<=x[j]&&x[j]<i+K);

			bool q = (0<=y[j]&&y[j]<K);

			if( p ^ q == c[j] )

			{

				cnt++;

				y1[y[j]] ++;

			}

			else 

			{

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) 

		{

			Max=std::max(Max,std::max(cnt,N-cnt));

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<cstring>

const int MAXN = 100000;

const int MAXK = 1000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[10 * MAXK + 5];

int main() 

{

	int N,K,Max=0,cnt = 0;

	scanf("%d%d",&N,&K);

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

	{

		char ch;

		scanf("%d%d %c",&x[i],&y[i],&ch);

		x[i]= x[i] % (2*K);

		y[i]= y[i] % (2*K);

		if(ch=='W' ) c[i] = 1;

		else c[i]=0;

	}

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

		cnt = 0;

		memset(y1,0,sizeof(y1));

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

			bool p = ( i <= x[j] && x[j] < i+K );

			bool q = ( 0 <= y[j] && y[j] < K );

			if( p ^ q == c[j] )

			{

				cnt++;

				y1[y[j]] ++;

			}

			else 

			{

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) 

		{

			Max=std::max(Max,std::max(cnt,N-cnt));

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}


// Faster C++ solution for the same problem (measured speedup: 2.224462869906131x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int MAXN = 100000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[MAXN],N,K,Max,cnt;

int main() 

{

	scanf("%d%d",&N,&K);

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

	{

		char ch;

		scanf("%d %d %c",&x[i],&y[i],&ch);

		x[i]%=(2*K);

		y[i]%=(2*K);

		if(ch=='W' ) c[i] = 1;

	}

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

		cnt = 0;

		fill(y1,y1+2*K+1,0);

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

			bool p = (i<=x[j]&&x[j]<i+K);

			bool q = (0<=y[j]&&y[j]<K);

			if( p ^ q == c[j] )

			{

				cnt++;

				y1[y[j]] ++;

			}

			else 

			{

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) 

		{

			Max=std::max(Max,std::max(cnt,N-cnt));

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

const int MAXN = 100000;

const int MAXK = 1000;

int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];

int y1[10 * MAXK + 5];

int main() {

	int N, K, Max = 0, cnt = 0;

	scanf("%d%d",&N,&K);

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

		char ch;

		scanf("%d%d %c",&x[i],&y[i],&ch);

		x[i] = x[i] % (2*K);

		y[i] = y[i] % (2*K);

		if( ch == 'W' ) c[i] = 1;

		else c[i] = 0;

	}

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

		cnt = 0;

		memset(y1,0,sizeof(y1));

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

			int p = ( i <= x[j] && x[j] < i+K );

			int q = ( 0 <= y[j] && y[j] < K );

			if( p ^ q == c[j] ){

				cnt++;

				y1[y[j]] ++;

			}

			else {

				y1[y[j]] --;

			}

		}

		for(int j=0;j<=K;j++) {

			if( cnt > Max ) Max = cnt;

			if( N - cnt > Max ) Max = N - cnt;

			if( j != K )

				cnt = cnt - y1[j] - y1[K+j];

		}

	}

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

}

// Faster C++ solution for the same problem (measured speedup: 2.224462869906131x):
#include<cstdio>

#include<algorithm>

using namespace std;

#define N 100000

int x[N+5],y[N+5],c[N+5],y1[N+5];

int n,k,ans,cnt;

int main()

{

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

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

	{

		char ch;

		scanf("%d %d %c",&x[i],&y[i],&ch);

		x[i]%=(2*k);

		y[i]%=(2*k);

		if(ch=='W')

			c[i]=1;

	}

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

	{

		cnt=0;

		fill(y1,y1+2*k+1,0);

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

		{

			bool p=(i<=x[j]&&x[j]<i+k);

			bool q=(0<=y[j]&&y[j]<k);

			if(p^q==c[j])

				cnt++,y1[y[j]]++;

			else

				y1[y[j]]--;

		}

		for(int j=0;j<=k;j++)

		{

			ans=max(max(cnt,n-cnt),ans);

			if(j!=k)

				cnt=cnt-y1[j]-y1[k+j];

		}

	}

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

}
// Slower C++ solution:
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

int n,k;

int x[100001],y[100001];

bool c[100001];

char s;

int h[10001];

int  main()

{

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

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

	{

		scanf("%d%d %c",&x[i],&y[i],&s);

		x[i]=x[i]%(2*k);

		y[i]=y[i]%(2*k);

		if(s=='B')

			c[i]=1;

	}

	int maxn=0,cnt;

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

	{

		cnt=0;

		memset(h,0,sizeof(h));

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

		{

			int p=(i<=x[j]&&x[j]<i+k);

			int q=(0<=y[j]&&y[j]<k);

			if(p^q==c[j])

			{

				cnt++;

				h[y[j]]++;

			}

			else

				h[y[j]]--;

		}

		for(int j=0;j<=k;j++)

		{

			maxn=max(max(cnt,n-cnt),maxn);

			if(j!=k)

				cnt=cnt-h[j]-h[k+j];

		}

	}

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

}

// Faster C++ solution for the same problem (measured speedup: 2.2243529227220087x):
#include<cstdio>

#include<algorithm>

using namespace std;

#define N 100000

int x[N+5],y[N+5],c[N+5],y1[N+5];

int n,k,ans,cnt;

int main()

{

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

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

	{

		char ch;

		scanf("%d %d %c",&x[i],&y[i],&ch);

		x[i]%=(2*k);

		y[i]%=(2*k);

		if(ch=='W')

			c[i]=1;

	}

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

	{

		cnt=0;

		fill(y1,y1+2*k+1,0);

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

		{

			bool p=(i<=x[j]&&x[j]<i+k);

			bool q=(0<=y[j]&&y[j]<k);

			if(p^q==c[j])

				cnt++,y1[y[j]]++;

			else

				y1[y[j]]--;

		}

		for(int j=0;j<=k;j++)

		{

			ans=max(max(cnt,n-cnt),ans);

			if(j!=k)

				cnt=cnt-y1[j]-y1[k+j];

		}

	}

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

}
// Slower C++ solution:




#include<bits/stdc++.h>

using namespace std;

#define MAX 100005

#define INF (1e9+1)

 

int n,W;

int dp[MAX];

 

int main(){

  fill(dp,dp+MAX,INF);

  dp[0]=0;

   

  cin>>n>>W;

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

    int v,w;

    cin>>v>>w;

    for(int j=MAX-1;j>=v;j--){

      dp[j]=min(dp[j],dp[j-v]+w);

    }

  }

 

  for(int i=MAX-1;i>=0;i--){

    if(dp[i]<=W){

      cout<<i<<endl;

      break;

    }

  }

   

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.485720886325435x):
#include<iostream>

#include<cstdio>

#include<algorithm>

using namespace std;

 

int main()

{

    int i,j,N,v[100],ans;

    long long w[100],W,dp[101][10001];

     

    cin >> N >> W;

     

    for(i=0;i<=N-1;i++) cin >> v[i] >> w[i];

     

    for(i=0;i<=100*N;i++) dp[0][i]=1000000001;

    dp[0][0]=0;

    for(i=1;i<=N;i++)

    {

        for(j=0;j<=N*100;j++)

        {

            if(j-v[i-1]>=0) dp[i][j]=min(dp[i-1][j], dp[i-1][j-v[i-1]]+w[i-1]);

            else dp[i][j]=dp[i-1][j];

        }

    }

    for(i=0;i<=N*100;i++)

    {

        if(dp[N][i]<=W) ans=i;

    }

    cout << ans << endl;

    return 0;

}