#include <bits/stdc++.h>
using namespace std;
const int maxx = 1e5 + 7;
const int Inf = 1 << 30;
const long long INF = 1ll << 60;
int n, m;
int a[25], b[25];
int dd[25];
int ans;
int Cnt(int x) {
  if (!x) return 1;
  int res = 0;
  while (x) {
    res++;
    x /= 10;
  }
  return res;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d %d", &a[i], &b[i]);
  if (n == 1) {
    for (int i = 1; i <= m; i++) ans = min(ans, b[i]);
  } else if (n == 2) {
    int num = 0;
    int x = 0, y = 0;
    for (int j = 1; j <= m; j++) {
      if (a[j] == 1) dd[++num] = b[j];
    }
    sort(dd + 1, dd + num + 1);
    for (int j = 1; j <= num; j++) {
      if (dd[j]) {
        x = dd[j];
        break;
      }
    }
    num = 0;
    memset(dd, 0, sizeof(dd));
    for (int j = 1; j <= m; j++) {
      if (a[j] == 2) dd[++num] = b[j];
    }
    sort(dd + 1, dd + num + 1);
    y = dd[1];
    ans = x * 10 + y;
  } else if (n == 3) {
    int num = 0;
    int x = 0, y = 0, z = 0;
    for (int j = 1; j <= m; j++) {
      if (a[j] == 1) dd[++num] = b[j];
    }
    sort(dd + 1, dd + num + 1);
    for (int j = 1; j <= num; j++) {
      if (dd[j]) {
        x = dd[j];
        break;
      }
    }
    num = 0;
    memset(dd, 0, sizeof(dd));
    for (int j = 1; j <= m; j++) {
      if (a[j] == 2) dd[++num] = b[j];
    }
    sort(dd + 1, dd + num + 1);
    y = dd[1];
    num = 0;
    memset(dd, 0, sizeof(dd));
    for (int j = 1; j <= m; j++) {
      if (a[j] == 3) dd[++num] = b[j];
    }
    sort(dd + 1, dd + num + 1);
    z = dd[1];
    ans = x * 100 + y * 10 + z;
  }
  if (Cnt(ans) == n)
    printf("%d\n", ans);
  else
    puts("-1");
  return 0;
}
