#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, p = 0;
  cin >> n >> m;
  int a[m][2], b[n];
  for (i = 0; i < n; i++) b[i] = 10;
  for (i = 0; i < m; i++)
    for (j = 0; j < 2; j++) cin >> a[i][j];
  for (i = 0; i < m; i++) {
    for (j = 0; j < 2; j++) {
      if (a[i][0] == 1) {
        if (b[0] == 10)
          b[0] = a[i][1];
        else if (b[0] == a[i][1])
          b[0] = a[i][1];
        else {
          p = -1;
          goto jump;
        }
      }
      if (a[i][0] == 2) {
        if (b[1] == 10)
          b[1] = a[i][1];
        else if (b[1] == a[i][1])
          b[1] = a[i][1];
        else {
          p = -1;
          goto jump;
        }
      }
      if (a[i][0] == 3) {
        if (b[2] == 10)
          b[2] = a[i][1];
        else if (b[2] == a[i][1])
          b[2] = a[i][1];
        else {
          p = -1;
          goto jump;
        }
      }
    }
  }
  for (i = 0; i < n; i++) {
    if (b[i] == 10 && i != 0) b[i] = 0;
    if (i == 0 && n != 1 && (b[0] == 0 || b[0] == 10)) {
      p = -1;
      goto jump;
    }
  }
  if (n == 3)
    p = b[0] * 100 + b[1] * 10 + b[2];
  else if (n == 2)
    p = b[0] * 10 + b[1];
  else {
    if (b[i] == 10) p = 0;
  }
jump : {};
  cout << p;
}
