#include <bits/stdc++.h>
using namespace std;
int n, x, y, m;
int a[15];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  set<int> s1;
  set<int> s2;
  set<int> s3;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    a[x] = y;
    if (x == 1 && y) s1.insert(y);
    if (x == 2) s2.insert(y);
    if (x == 3) s3.insert(y);
  }
  if (s1.size() > 0) {
    if (n > 0) cout << *(s1.begin());
    if (n > 1) {
      if (s2.size())
        cout << *(s2.begin());
      else
        cout << "0";
    }
    if (n == 3) {
      if (s3.size())
        cout << *(s3.begin());
      else
        cout << "0" << endl;
    }
    cout << endl;
    return 0;
  }
  cout << "-1" << endl;
  return 0;
}
