#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long N, M;
  cin >> N >> M;
  long long s[M], c[M];
  for (long long(i) = 0; (i) < (M); (i)++) cin >> s[i] >> c[i];
  long long n3[3] = {-1, -1, -1};
  bool E = false;
  for (long long(i) = 0; (i) < (N); (i)++) {
    int n = s[i] - 1;
    if (n3[n] == -1)
      n3[n] = c[i];
    else if (n3[n] == c[i])
      continue;
    else {
      E = true;
      break;
    }
  }
  if (E)
    cout << -1;
  else if (n3[0] == -1)
    cout << -1;
  else {
    for (long long(i) = 0; (i) < (3); (i)++) cout << (n3[i] == -1 ? 0 : n3[i]);
    cout << endl;
  }
  return 0;
}
