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

int main(){
  int a;
  cin>>a;
  int b;
  cin>>b;  
  vector<int> vec(b*2);
  for (int i = 0; i < b*2; i++) {
    cin >> vec.at(i);
  }
  int count=0;
  vector<int> vec2(3);
  vec2[0]=0;
  vec2[1]=0;
  vec2[2]=0;
  int error1=0;
   int error2=0;
    int error3=0;
  int charenge1 =0;
    int charenge2 =0;
      int charenge3 =0;
  
  
  
  
  for (int n = 0; n < b*2; n++) { 
    if(n%2==0){
      if(vec[n]==1){
          if(vec2[0]==0) vec2[0]=vec[n+1]*pow(10,(a-1));
          else if(vec2[0]>0){
              charenge1++;
             error1 = vec2[0]-vec[n+1]*pow(10,(a-1));
          }
            }
      if(vec[n]==2){
            if(vec2[1]==0) vec2[1]=vec[n+1]*pow(10,(a-2)); 
            else if(vec2[1]>0){
                charenge2++;
             error2 = vec2[1]-vec[n+1]*pow(10,(a-2));
          }
          }
      if(vec[n]==3){
           if(vec2[2]==0) vec2[2]=vec[n+1]*pow(10,(a-3));
            else if(vec2[2]>0){
                charenge3++;
             error3 = vec2[2]-vec[n+1]*pow(10,(a-3));
          }
          }
    }
  }
    for (int n = 0; n < b*2; n++) { 
    if(n%2==0){
      if(vec[n]==a){
        int root =vec[n+1];
      }
    }
    }
  
  count = vec2[0]+vec2[1]+vec2[2];
  if(charenge1>0 && error1!=0)cout <<"-1"<<endl;
  else if(charenge2>0 && error2!=0)cout <<"-1"<<endl;
  else if(charenge3>0 && error3!=0)cout <<"-1"<<endl;
  else if(count==0)cout <<"-1"<<endl;
  else if(root==0)cout <<"-1"<<endl;
  else{cout <<count<<endl;}
 
  
}