#include <bits/stdc++.h>
using namespace std;
int main(void) {
  string str;
  int n, m;
  int b, c;
  int a[3] = {};
  int flag;
  getline(cin, str);
  istringstream s(str);
  s >> n >> m;
  flag = 0;
  for (int i = 0; i < m; i++) {
    getline(cin, str);
    istringstream s(str);
    s >> b >> c;
    if (a[b - 1] >= c || a[b - 1] == 0) {
      if (3 - n == b - 1 && c == 0) continue;
      a[b - 1] = c;
    }
  }
  if (a[3 - n] != 0 && flag == 0) {
    cout << a[0] * 100 + a[1] * 10 + a[2] << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
