#include <bits/stdc++.h>
using namespace std;
int main() {
  while (true) {
    int n, m, a1, a2, a3, h;
    a1 = 0;
    a2 = 0;
    a3 = 0;
    h = 0;
    cin >> n >> m;
    if (n != 3) {
      cout << "-1" << endl;
      h = 1;
    }
    if (h == 1) break;
    int s[m], c[m];
    for (int i = 0; i < m; i++) {
      cin >> s[i] >> c[i];
      if (s[i] == 1) a1 = c[i];
      if (s[i] == 2) a2 = c[i];
      if (s[i] == 3) a3 = c[i];
      if (s[i] == 1 && c[i] == 0) {
        cout << "-1" << endl;
        h = 1;
      }
    }
    if (h == 1) break;
    if (s[0] == s[1]) {
      if (c[0] != c[1]) {
        cout << "-1" << endl;
        h = 1;
      }
    }
    if (h == 1) break;
    if (s[1] == s[2]) {
      if (c[1] != c[2]) {
        cout << "-1" << endl;
        h = 1;
      }
    }
    if (h == 1) break;
    if (s[0] == s[2]) {
      if (c[0] != c[2]) {
        cout << "-1" << endl;
        h = 1;
      }
    }
    if (h == 1) break;
    cout << a1 << a2 << a3 << endl;
    break;
  }
}
