#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int main() {
	long n,m,y = 1;
  	cin >> n >> m;
  	vector<long> s(n);
  	vector<long> c(i);
  	for(long i = 0; i< n; i++){
      cin >> s[i];
      cin >> c[i];
    }
  	for(long i = 0; i< n;){
      if(s[i]==s[i+y)] && c[i]==c[i+1]){
        cout << -1 << endl;
        break;
      else{
        if(y<n){
          y++;
        }
        else{
          i++;
          n=n-1;
        }
      }
        
      }
      }
    }
}	