#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int s[3], c[m];
  int one = 0, two = 0, three = 0;
  int min = 10000;
  int first;
  for (int i = 0; i <= m - 1; ++i) {
    cin >> s[i] >> c[i];
    if (s[i] == 1) one = c[i];
    if (s[i] == 2) two = c[i];
    if (s[i] == 3) three = c[i];
    if (s[i] < min) {
      min = s[i];
      first = c[i];
    }
  }
  if (first == 0) {
    cout << "-1" << endl;
    return 0;
  }
  for (int i = 0; i <= m - 2; ++i) {
    for (int j = i + 1; j <= m - 1; ++j) {
      if (s[i] == s[j]) {
        if (c[i] != c[j]) {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
  }
  int ans = 1;
  while (1) {
    if (ans / 100 >= one && (ans % 100) / 10 >= two && ans % 10 >= three) {
      cout << ans << endl;
      return 0;
    }
    ans++;
  }
  return 0;
}
