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