#include <bits/stdc++.h>
#define REP(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
using P = pair<int,int>;
typedef long long ll;
const ll mod = 1e9+7;
#define all(x) (x).begin(),(x).end()

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(10);
    int N,M;
    cin >> N >> M;
    vector<P> s(M);
    REP(i,M){
        cin >> s[i].first;
        cin >> s[i].second;
    }
    sort(all(s),greater<>());
    int top = 10,sec = 10,thi = 10;
    bool ok = 1;
    REP(i,M){
        if(s[i].first == 1){
            if(s[i].second == 0){
                if(N != 1)ok = 0;
            }
            if(top == 10){
                top = s[i].second;
            }else if(top != s[i].second){
                ok = 0;
            }
        }else if(s[i].first == 2){
            if(sec == 10){
                sec = s[i].second;
            }else if(sec != s[i].second){
                ok = 0;
            }
        }else{
            if(thi == 10){
                thi = s[i].second;
            }else if(thi != s[i].second){
                ok = 0;
            }
        }
    }
    if(top == 10)top = 1;
    if(sec == 10)sec = 0;
    if(thi == 10)thi = 0;
    if(N == 3 && ok == 1)cout <<top<<sec<<thi<< "\n";
    if(N == 2 && ok == 1)cout <<top<<sec<< "\n";
    if(N == 1 && ok == 1)cout <<top<< "\n";
    if(ok == 0)cout <<-1<< "\n";
}