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