#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> vec(M, vector<int>(2));
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> vec.at(i).at(j);
    }
  }
  int ans;
  int a, b, c;
  a = -1, b = -1, c = -1;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < 2; j++) {
      if (vec.at(i).at(0) == 1 && vec.at(i).at(1) == 0) {
        if (N > 1) {
          cout << -1;
          return 0;
        }
      }
      if (vec.at(i).at(0) == 1) {
        if (a == -1) {
          a = vec.at(i).at(1);
        } else if (a != vec.at(i).at(1)) {
          cout << -1;
          return 0;
        } else {
          a = vec.at(i).at(1);
        }
      }
      if (vec.at(i).at(0) == 2) {
        if (b == -1) {
          b = vec.at(i).at(1);
        } else if (a != vec.at(i).at(1)) {
          cout << -1;
          return 0;
        } else {
          b = vec.at(i).at(1);
        }
      }
      if (vec.at(i).at(0) == 3) {
        if (c == -1) {
          c = vec.at(i).at(1);
        } else if (c != vec.at(i).at(1)) {
          cout << -1;
          return 0;
        } else {
          c = vec.at(i).at(1);
        }
      }
    }
  }
  if (b == -1) {
    b = 0;
  }
  if (a == -1) {
    a = 0;
  }
  if (c == -1) {
    c = 0;
  }
  ans = a * 100 + b * 10 + c;
  cout << ans;
  return 0;
}
