#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int s[5], c[5];
  cin >> n >> m;
  for (int i = 0; i < m; i++) cin >> s[i] >> c[i];
  int l = 10000;
  int q;
  if (n == 1) {
    for (int i = 1; i < 10; i++) {
      q = 0;
      for (int j = 0; j < m; j++) {
        if (s[j] == 1 && c[j == i]) q++;
      }
      if (q == m) l = min(l, i);
    }
  } else if (n == 2) {
    for (int i = 10; i < 100; i++) {
      q = 0;
      for (int j = 0; j < m; j++) {
        if (s[j] = 1 && c[j] == i / 10)
          q++;
        else if (s[j] = 2 && c[j] == i % 10)
          q++;
      }
      if (q == m) l = min(l, i);
    }
  } else if (n == 3) {
    for (int i = 100; i < 1000; i++) {
      q = 0;
      for (int j = 0; j < m; j++) {
        if (s[j] == 1 && c[j] == i / 100)
          q++;
        else if (s[j] == 2 && c[j] == (i / 10) % 10)
          q++;
        else if (s[j] == 3 && c[j] == i % 10)
          q++;
      }
      if (q == m) l = min(l, i);
    }
  }
  if (l == 10000) l = -1;
  cout << l << endl;
}
