#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++) {
    cin >> vec[i][0] >> vec[i][1];
  }
  if (N > 1) {
    for (int i = 0; i < M; i++) {
      if (vec[i][0] == 1 && vec[i][1] == 0) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  for (int i = 0; i < M; i++) {
    for (int j = i + 1; j < M; j++) {
      if (vec[i][0] == vec[j][0] && vec[i][1] != vec[j][1]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  int a = 0;
  int b = 0;
  int c = 0;
  for (int i = 0; i < M; i++) {
    if (vec[i][0] == 1) {
      a = vec[i][1];
    } else if (vec[i][0] == 2) {
      b = vec[i][1];
    } else if (vec[i][0] == 3) {
      c = vec[i][1];
    }
  }
  if (a == 0 && b == 0 && c == 0) {
    cout << 0 << endl;
  } else if (a == 0 && b == 0 && c != 0) {
    cout << c << endl;
  } else if (a == 0 && b != 0) {
    cout << b << c << endl;
  } else if (a != 0) {
    cout << a << b << c << endl;
  }
}
