#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
typedef long long ll;
#define _GLIBCXX_DEBUG
vector<int>a[8];
void digit(int x){
  while(x){
    a[i]=x%10;
    x/=10;
  }
}
int main()	{
  int n,m;
  cin>>n>>m;
  vector<int>s[m],c[m];
  rep(i,m) cin>>s[i]>>c[i];
  rep(i,1000){
    rep(i,m){
      rep(j,m){
        if(j==s[i] && a[j]==c[i]){
          cout<<x<<endl;
          return 0;
        }
      }
    }
  }
  cout<<-1<<endl;
}
