#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int s, c;
  int a, b, d;
  a = b = d = 10;
  for (int i = 0; i < m; i++) {
    cin >> s >> c;
    if (s == 1) a = min(a, c);
    if (s == 2) b = min(b, c);
    if (s == 3) d = min(d, c);
  }
  if (n == 1) {
    if (a != 10)
      cout << a << "\n";
    else
      cout << "-1\n";
  } else if (n == 2) {
    if (a == 10 || a == 0) {
      cout << "-1\n";
    } else {
      if (b == 10) b = 0;
      cout << a << b << "\n";
    }
  } else {
    if (a == 10 || a == 0)
      cout << "-1\n";
    else {
      if (b == 10) b = 0;
      if (d == 10) d = 0;
      cout << a << b << d << "\n";
    }
  }
}
