#include <bits/stdc++.h>
int main() {
  int i, n, m;
  int keta[100];
  int kazu[100];
  scanf("%d %d", &n, &m);
  if (n == 0) {
    n = 1;
  }
  for (i = 0; i < m; i++) {
    scanf("%d", &keta[i]);
    scanf("%d", &kazu[i]);
  }
  int kazumin[3];
  int count = 0, ke = 0;
  while (count < m) {
    while (ke < 3) {
      if (count + 1 == keta[ke]) {
        kazumin[count] = kazu[ke];
        break;
      }
      ke++;
    }
    ke = 0;
    for (i = 0; i < m; i++) {
      if (kazumin[count] > kazu[i] && count + 1 == keta[i]) {
        kazumin[count] = kazu[i];
      }
    }
    count++;
  }
  int number = kazumin[0] * 100 + kazumin[1] * 10 + kazumin[2];
  for (i = 0; i < 3 - n; i++) {
    number /= 10;
  }
  if (kazumin[0] == 0) {
    printf("-1\n");
  } else {
    printf("%d\n", number);
  }
  return 0;
}
