#include <bits/stdc++.h>
using namespace std;
int num[5];
int main(void) {
  int n, m, f = 1;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int s, c;
    scanf("%d%d", &s, &c);
    if (num[s] == 0)
      num[s] = c;
    else if (num[s]) {
      if (num[s] == c)
        continue;
      else {
        f = 0;
        break;
      }
    }
  }
  if (f == 0) {
    printf("-1\n");
    return 0;
  }
  for (int i = 1; i <= 3; i++) {
    if (f == 1 && num[i] == 0) {
      printf("-1\n");
      break;
    } else {
      f = 2;
      printf("%d", num[i]);
    }
  }
  return 0;
}
