#include <bits/stdc++.h>
using namespace std;
const int M = 5;
int main() {
  int n, m;
  int num[4] = {-1, -1, -1, -1};
  cin >> n >> m;
  bool flag = false;
  for (int i = 0; i < m; i++) {
    int s, c;
    cin >> s >> c;
    if (num[s] < c) {
      num[s] = c;
      if ((4 - s == n && c != 0) || (s == 3 && n == 1)) {
        flag = true;
      }
      if (4 - s == n && c == 0 && n != 1) {
        flag = false;
      }
    }
  }
  for (int i = 0; i < 4; i++) {
    if ((num[i] != -1 && num[1] != 0) && 4 - i > n) flag = false;
    if (num[i] == -1) num[i] = 0;
  }
  if (!flag) {
    cout << -1 << endl;
  } else {
    cout << num[1] * 100 + num[2] * 10 + num[3] << endl;
  }
  return 0;
}
