#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> A(M, vector<int>(2));
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> A.at(i).at(j);
    }
  }
  int x[3] = {0};
  int flag[3] = {0};
  int flag2 = 0;
  for (int i = 0; i < M; i++) {
    if (flag[A.at(i).at(0) - 1] == 0) {
      x[A.at(i).at(0) - 1] = A.at(i).at(1);
      flag[A.at(i).at(0) - 1] = 1;
    } else if (flag[A.at(i).at(0) - 1] == 1 &&
               x[A.at(i).at(0) - 1] != A.at(i).at(1)) {
      flag2 = 1;
    }
  }
  if (x[0] == 0) {
    if (N = 3) {
      flag2 = 1;
    }
  }
  if (x[0] == 0 && x[1] == 0) {
    if (N = 2) {
      flag2 = 1;
    }
  }
  if (x[0] == 0 && x[1] == 0 && x[2] == 0) {
    if (N = 1) {
      flag2 = 1;
    }
  }
  if (flag2 == 0) {
    cout << x[0] * 100 + x[1] * 10 + x[2] << endl;
  } else {
    cout << -1 << endl;
  }
}
