#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s;
  vector<int> c;
  for (int i = 0; i < m; i++) {
    int S, C;
    cin >> S >> C;
    s.push_back(S);
    c.push_back(C);
  }
  int a = 10;
  bool a_f = false;
  int b = 10;
  bool b_f = false;
  int d = 10;
  bool d_f = false;
  for (int i = 0; i < m; i++) {
    if (s[i] == 1 && a_f != true) {
      a = min(a, c[i]);
      a_f = true;
    } else if (s[i] == 1 && a_f && a != c[i]) {
      cout << -1 << endl;
      return 0;
    } else if (s[i] == 2 && b_f != true) {
      b = min(b, c[i]);
      b_f = true;
    } else if (s[i] == 2 && b_f && b != c[i]) {
      cout << -1 << endl;
      return 0;
    } else if (s[i] == 3 && d_f != true) {
      d = min(d, c[i]);
      d_f = true;
    } else if (s[i] == 3 && d_f && d != c[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  if (a == 10) a = 0;
  if (b == 10) b = 0;
  if (d == 10) d = 0;
  if (a == 0 && b == 0 && d == 0) {
    cout << -1 << endl;
    return 0;
  }
  if (a == 0 && b != 0 && d != 0) {
    cout << b << d << endl;
  } else if (a == 0 && b == 0 && d != 0) {
    cout << d << endl;
  } else {
    cout << a << b << d << endl;
  }
}
