#include <bits/stdc++.h>
using ll = long long;
int main(int argc, char** argv) {
  int n, m;
  std::cin >> n >> m;
  int ans = -1;
  int d[] = {0, 0, 0};
  if (m == 0) {
    std::printf("0\n");
    return 0;
  }
  if (n > m) {
    std::printf("%d\n", ans);
    return 0;
  }
  int flg[] = {0, 0, 0};
  for (int i = 0; i < m; i++) {
    int s, c;
    std::cin >> s >> c;
    if (flg[s - 1] == 1) {
      continue;
    }
    if (d[s - 1] != 0) {
      d[s - 1] = std::min(c, d[s - 1]);
    } else {
      d[s - 1] = c;
      flg[s - 1] = c == 0 ? 1 : 0;
    }
  }
  std::printf("%d\n", d[0] * 100 + d[1] * 10 + d[2]);
  return 0;
}
