#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int s[m], c[m];
  int a = 0, b = 0, d = 0;
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> c[i];
    if (s[i] == 1) a = c[i];
    if (s[i] == 2) b = c[i];
    if (s[i] == 3) d = c[i];
  }
  for (int i = 0; i < m; i++) {
    if (s[i] == 1) {
      if (c[i] == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (s[i] == 1) {
      if (c[i] != 0 && a == c[i]) {
        a = c[i];
      }
      if (a != c[i]) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (s[i] == 2) {
      if (b == c[i]) {
        b = c[i];
      }
      if (b != c[i]) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (s[i] == 3) {
      if (d == c[i]) {
        d = c[i];
      }
      if (d != c[i]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << a << b << d << endl;
  return 0;
}
