#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, n1, n2, n3;
  cin >> n >> m;
  n1 = 10;
  n2 = 10;
  n3 = 10;
  int s, c;
  int f1, f2, f3;
  f1 = 0;
  f2 = 0;
  f3 = 0;
  for (int i = 0; i < m; i++) {
    cin >> s >> c;
    if (s == 1) {
      if (n1 == 10) {
        n1 = c;
      } else {
        if (n1 != c) {
          cout << -1 << endl;
          exit(0);
        }
      }
    } else if (s == 2) {
      if (n2 == 10) {
        n2 = c;
      } else {
        if (n2 != c) {
          cout << -1 << endl;
          exit(0);
        }
      }
    } else if (s == 3) {
      if (n3 == 10) {
        n3 = c;
      } else {
        if (n3 != c) {
          cout << -1 << endl;
          exit(0);
        }
      }
    }
  }
  if (n1 == 10 || n1 == 0) {
    if (n2 == 10 || n2 == 0) {
      if (n3 != 10) {
        cout << n3 << endl;
        exit(0);
      } else {
        cout << -1 << endl;
        exit(0);
      }
    }
  }
  if (n2 == 10) n2 = 0;
  if (n3 == 10) n3 = 0;
  int ans = n1 * 100 + n2 * 10 + n3;
  cout << ans << endl;
}
