#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int dex[3] = {0, 0, 0};
  for (int i = 0; i < M; i++) {
    int digit, num;
    cin >> digit >> num;
    if (dex[digit - 1] == 0 || dex[digit - 1] == num) {
      dex[digit - 1] = num;
    } else {
      cout << "-1" << endl;
      return 0;
    }
  }
  if (dex[0] + dex[1] + dex[2]) {
    cout << dex[0] << dex[1] << dex[2] << endl;
  } else {
    cout << "-1" << endl;
    return 0;
  }
}
