#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, s, c, onec = 0, twoc = 0, threec = 0;
  bool success = true;
  cin >> N >> M;
  int A[N];
  while (M--) {
    cin >> s >> c;
    if (s == 1) {
      onec++;
      if (onec > 1 && c != A[0]) {
        success = false;
      } else {
        A[0] = c;
      }
    } else if (s == 2) {
      twoc++;
      if (twoc > 1 && c != A[1]) {
        success = false;
      } else {
        A[1] = c;
      }
    } else if (s == 3) {
      threec++;
      if (threec > 1 && c != A[2]) {
        success = false;
      } else {
        A[2] = c;
      }
    }
  }
  if ((A[0] == 0 && (A[1] != 0 || A[2] != 0)) ||
      (A[0] == 0 && A[1] == 0) && A[2] != 0) {
    cout << -1 << endl;
  } else if (success) {
    for (int i = 0; i < 3; i++) {
      cout << A[i];
    }
  } else {
    cout << -1 << endl;
  }
}
