#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<pair<int, int>> x(100), y(100);
  int n, m, o, t, h, k = 0;
  o = t = h = 10;
  cin >> n >> m;
  vector<int> s(m), c(m);
  for (int i = 0; i < m; ++i) {
    cin >> s.at(i) >> c.at(i);
    if (s.at(i) == 1) {
      o = min(o, c.at(i));
    }
    if (s.at(i) == 2) {
      t = min(t, c.at(i));
    }
    if (s.at(i) == 3) {
      h = min(h, c.at(i));
    }
    x.at(s.at(i)).first += 1;
    if (x.at(s.at(i)).first > 0 && c.at(i) == x.at(s.at(i)).second) {
      x.at(s.at(i)).first -= 1;
      x.at(s.at(i)).second = c.at(i);
    }
    if (x.at(s.at(i)).second == 0)
      x.at(s.at(i)).second = c.at(i);
    else if (x.at(s.at(i)).second != c.at(i)) {
      cout << "-1" << endl;
      return 0;
    }
    if (x.at(s.at(i)).second == c.at(i)) x.at(s.at(i)).first = 1;
    if (x.at(s.at(i)).second != c.at(i)) k = 1;
    if (s.at(i) == 1 && c.at(i) == 0　&& n != 0) {
      cout << "-1" << endl;
      return 0;
    }
    if (x.at(s.at(i)).first != 0)
      if (x.at(s.at(i)).second != c.at(i)) {
        cout << "-1" << endl;
        return 0;
      }
  }
  if (o == 10) {
    o = 1;
    if (n == 1) o = 0;
  }
  if (t == 10) {
    t = 0;
  }
  if (h == 10) h = 0;
  cout << o;
  if (n == 2) cout << t;
  if (n == 3) cout << t << h;
  cout << endl;
}
