#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i;
  cin >> n >> m;
  set<int> s1, s2, s3;
  set<int>::iterator it, ti, iu;
  for (i = 0; i < m; i++) {
    int s, c;
    cin >> s >> c;
    if (s == 1)
      s1.insert(c);
    else if (s == 2)
      s2.insert(c);
    else
      s3.insert(c);
  }
  if (s1.size() != 1 || s2.size() > 1 || s3.size() > 1) {
    cout << "-1";
    return 0;
  }
  it = s1.begin();
  if (*it == 0 && n > 1) {
    cout << "-1";
    return 0;
  }
  cout << *it;
  ti = s2.begin();
  if (n >= 2) {
    if (s2.size() == 0)
      cout << "0";
    else
      cout << *ti;
  }
  iu = s3.begin();
  if (n == 3) {
    if (s3.size() == 0)
      cout << "0";
    else
      cout << *iu;
  }
}
