#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> c(3, 0);
  int temp[2];
  bool f = true;
  for (int i = 0; i < m; i++) {
    cin >> temp[0] >> temp[1];
    if (c[temp[0] - 1] != 0 && temp[1] != c[temp[0] - 1]) {
      f = false;
      break;
    }
    c[temp[0] - 1] = temp[1];
  }
  if (f && c[3 - n] != 0) {
    cout << c[0] << c[1] << c[2] << endl;
  } else {
    cout << "-1" << endl;
  }
  return 0;
}
