#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, s[6], c[6];
  cin >> n >> m;
  int fst = 0, scnd = 0, thrd = 0;
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> c[i];
  }
  int x = 10, y = 10, z = 10;
  int k = 0;
  for (int i = 0; i < m; i++) {
    if (s[i] == 1) {
      x = min(x, c[i]);
      k = 1;
    }
    if (s[i] == 2) {
      y = min(y, c[i]);
    }
    if (s[i] == 3) {
      z = min(z, c[i]);
    }
  }
  if (n == 1) {
    if (x > 9)
      cout << 0 << endl;
    else
      cout << x << endl;
  } else if (n == 2) {
    if (k and x != 0) {
      cout << x;
      if (y > 9)
        cout << 0 << endl;
      else
        cout << y << endl;
    } else
      cout << -1 << endl;
  } else {
    if (k and x != 0) {
      cout << x;
      if (y > 9)
        cout << 0;
      else
        cout << y;
      if (z > 9)
        cout << 0 << endl;
      else
        cout << z << endl;
    } else
      cout << -1 << endl;
  }
  return 0;
}
