#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  int M;
  cin >> N;
  cin >> M;
  int check = 0;
  vector<vector<int>> data(M, vector<int>(2));
  for (int i = 0; i < M; i++) {
    cin >> data.at(i).at(0);
    cin >> data.at(i).at(1);
  }
  int first;
  int second;
  int third;
  int count = 0;
  int count2 = 0;
  int count3 = 0;
  for (int i = 0; i < M; i++) {
    if (N != 1 && data.at(i).at(0) == 1 && data.at(i).at(1) == 0) {
      check = 1;
    }
  }
  for (int i = 0; i < M; i++) {
    if (data.at(i).at(0) == 1) {
      if (count == 0) {
        first = data.at(i).at(1);
      } else {
        if (data.at(i).at(1) != first) {
          check = 1;
        }
      }
      count++;
    }
    if (data.at(i).at(0) == 2) {
      if (count2 == 0) {
        second = data.at(i).at(1);
      } else {
        if (data.at(i).at(1) != second) {
          check = 1;
        }
      }
      count2++;
    }
    if (data.at(i).at(0) == 3) {
      if (count3 == 0) {
        third = data.at(i).at(1);
      } else {
        if (data.at(i).at(1) != third) {
          check = 1;
        }
      }
      count3++;
    }
  }
  if (check == 1) {
    cout << -1 << endl;
  } else {
    if (count == 0 && N != 0) {
      first = 1;
    }
    if (count == 0 && N == 0) {
      first = 0;
    }
    if (count2 == 0) {
      second = 0;
    }
    if (count3 == 0) {
      third = 0;
    }
    if (N == 3) {
      cout << first << second << third << endl;
    } else if (N == 2) {
      cout << first << second << endl;
    } else if (N == 1) {
      cout << first << endl;
    }
  }
  return 0;
}
