#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int ans[4] = {};
  int kekka = 0;
  vector<vector<int>> data(5, vector<int>(2));
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < 2; j++) {
      cin >> data.at(i).at(j);
    }
  }
  if (N == 3) {
    for (int i = 0; i < M; i++) {
      if (data[0][i] == 1) {
        if (data[1][i] == 0) {
          cout << -1;
          break;
        }
      }
    }
  }
  if (N == 2) {
    for (int i = 0; i < M; i++) {
      if (data[0][i] == 1) {
        if (data[1][i] == 0) {
          cout << -1;
          break;
        }
      }
    }
  }
  for (int i = 0; i < M; i++) {
    ans[data[0][i]] = data[1][i];
    if (ans[data[0][i]] != data[1][i] && ans[data[0][i]] >= 0) {
      cout << -1;
      break;
    }
  }
  kekka = ans[1] * 100 + ans[2] * 10 + ans[3];
  cout << kekka;
  return 0;
}
