#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> s(M);
  vector<int> c(M);
  int ans = -1;
  int first = 0;
  int second = 0;
  int third = 0;
  for (int i = 0; i < M; i++) {
    cin >> s.at(i) >> c.at(i);
  }
  for (int i = 0; i < M - 1; i++) {
    if (s.at(i) == 1 && c.at(i) == 0) {
      break;
    }
    bool flg = false;
    for (int j = i + 1; j < M; j++) {
      if (s.at(i) == s.at(j) && c.at(i) != c.at(j)) {
        flg = true;
        break;
      }
    }
    if (flg) {
      break;
    }
    if (s.at(i) == 1) {
      first = c.at(i);
    }
    if (s.at(i) == 2) {
      second = c.at(i);
    }
    if (s.at(i) == 3) {
      third = c.at(i);
    }
  }
  if (first + second + third == 0) {
    cout << -1 << endl;
  } else {
    cout << first << second << third << endl;
  }
}
