#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  unordered_map<int, int> mp;
  for (int keta = 1; keta <= 3; keta++) mp[keta] = -1;
  for (int i = 0; i < (m); ++i) {
    int s, c;
    cin >> s >> c;
    if (mp[s] != -1 && mp[s] != c) {
      cerr << "s"
           << ": " << s << endl;
      cerr << "c"
           << ": " << c << endl;
      cout << -1 << endl;
      return 0;
    }
    mp[s] = c;
  }
  const int MAX_NUM = 999;
  int num;
  for (num = 0; num <= MAX_NUM; num++) {
    if (n == 1) {
      if (mp[1] != -1 || mp[2] != -1) {
        cerr << "mp[1]"
             << ": " << mp[1] << endl;
        cerr << "mp[2]"
             << ": " << mp[2] << endl;
        cout << -1 << endl;
        return 0;
      }
      if (mp[3] <= num) break;
    } else if (n == 2) {
      if (mp[1] != -1) {
        cerr << "mp[1]"
             << ": " << mp[1] << endl;
        cout << -1 << endl;
        return 0;
      }
      if (mp[2] <= num / 10 && mp[3] <= num % 10) break;
    } else {
      if (mp[1] <= num / 100 && mp[2] <= (num / 10) % 10 && mp[3] <= num % 10)
        break;
    }
  }
  cout << num << endl;
  return 0;
}
