//includes
#include<iostream>
#include<vector>
#include<set>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<map>
#include<queue>
#include<iomanip>
#include<numeric>
#include<assert.h>
#include<cstring>
#include<unordered_map>
#include<bitset>


template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//macros and consts
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define all(a) (a).begin(),(a).end()
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define FORE(i,a) for(auto &i : a)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define accm accumulate
#define Fi first
#define Se second
#define INF 1e9
#define llINF (1LL<<60)
#define mINF -1e9
#define mllINF -(1LL<<60)
#define endl "\n"
#define CONT continue
#define BRK break
#define modulo(n, m) (((n)%(m)+(m))%m)

//shorter types
using namespace std;
using ll = long long;
using vi = std::vector<int>;
using vc = std::vector<char>;
using vll = std::vector<long long>;
using vs = std::vector<string>;
using Mi = map<int,int>;
using Mll = map<ll,ll>;
using UMi = unordered_map<int,int>;
using UMll = unordered_map<ll,ll>;
using Pi = pair<int,int>;
using Pll = pair<ll,ll>;
using vPi = vector<Pi>;
using vPll = vector<Pll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using pqgi = priority_queue<int,vector<int>,greater<int>>;
using pqsi = priority_queue<int,vector<int>,less<int>>;
using pqgll = priority_queue<int,vector<int>,greater<int>>;
using pssll = priority_queue<int,vector<int>,less<int>>;
template<class T>
using vec = vector<T>;
#define int long long



int N,M;
//here begins your code
signed main(){
  cin >> N;
  cin >> M;

  if(N == 1){
    vector<int> numbers;
    for(int i = 0; i < M; i++){
      int s,c;
      cin >> s >> c;
      numbers.emplace_back(c);
    }
    sort(all(numbers));
    if(M == 0){
      cout << 0 << endl;
      return 0;
    }
    if(numbers[0] != numbers[numbers.size() - 1]){
      cout << -1 << endl;
      return 0;
    }
    else{
      cout << numbers[0] << endl;
    }
  }
  else if(N == 2){
    vector<vector<int>> numbers;
    numbers.resize(2);
    for(int i = 0; i < M; i++){
      int s,c;
      cin >> s >> c;
      s--;
      numbers[s].emplace_back(c);
    }
    for(int i = 0 ; i < 2 ; i++){
      sort(all(numbers[i]));
    }
    int min_result[2] = {1,0};
    if(M == 0){
      cout << 10 << endl;
      return 0;
    }
    for(int i = 0 ; i < 2 ; i++){
      if(numbers[i].size() == 0){
        continue;
      }
      if(numbers[i][0] != numbers[i][numbers[i].size() - 1]){
        cout << -1 << endl;
        return 0;

      }
      if((i == 0) && (numbers[i][0] == 0)){
        cout << -1 << endl;
        return 0;

      }
      min_result[i] = numbers[i][0];
    }
    cout << min_result[0] * 10 + min_result[1]<< endl;
  }
  else{
    vector<vector<int>> numbers;
    numbers.resize(3);
    for(int i = 0; i < M; i++){
      int s,c;
      cin >> s >> c;
      s--;
      numbers[s].emplace_back(c);
    }
    for(int i = 0; i < 3; i++){
      sort(all(numbers[i]));
    }
    int min_result[3] = {1,0,0};
    if(M == 0){
      cout << 100 << endl;
      return 0;
    }
    for(int i = 0 ; i < 3; i++){
      if(numbers[i].size() == 0){
        continue;
      }
      if(numbers[i][0] != numbers[i][numbers[i].size() - 1]){
        cout << -1 << endl;
        return 0;
      }
      if((i == 0) && (numbers[i][0] == 0)){
        cout << -1 << endl;
        return 0;
      }
      min_result[i] = numbers[i][0];
    }
    cout << min_result[0] * 100 + min_result[1] * 10 + min_result[2] << endl;
  }

  //check types
  //do not eb if you resize
  //resize before you access by idx
  return 0;
} 

Measured memory_bytes: 256