#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
int main() {
  int N, M;
  vector<int> s(M);
  vector<int> c(M);
  cin >> N >> M;
  for (int i = 0; i < M; i++) cin >> s.at(i) >> c.at(i);
  int a, b, d;
  a = 0;
  b = 0;
  d = 0;
  for (int i = 0; i < M; i++) {
    if (s.at(i) == 1) {
      if (a == 0) {
        a = c.at(i);
      } else if (a != c.at(i)) {
        cout << "-1" << endl;
        return 0;
      }
    }
    if (s.at(i) == 2) {
      if (b == 0) {
        b = c.at(i);
      } else if (b != c.at(i)) {
        cout << "-1" << endl;
        return 0;
      }
    }
    if (s.at(i) == 3) {
      if (d == 0) {
        d = c.at(i);
      } else if (d != c.at(i)) {
        cout << "-1" << endl;
        return 0;
      }
    }
  }
  if (N == 2 && a == 0 && b == 0) {
    cout << "-1" << endl;
    return 0;
  }
  if (N == 3 && a == 0) {
    cout << "-1" << endl;
    return 0;
  }
  cout << a << b << d << endl;
}
