#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);
  }
  if (N == 1) {
    for (int i = 0; i < M; i++) {
      if (data.at(0).at(1) != data.at(i).at(1)) {
        check = 1;
      }
    }
    if (check == 1) {
      cout << -1 << endl;
    } else if (check == 0) {
      cout << data.at(0).at(1) << endl;
    }
  }
  if (N == 2 || N == 3) {
    int first;
    int second;
    int third;
    int count = 0;
    int count2 = 0;
    int count3 = 0;
    for (int i = 0; i < M; i++) {
      if (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++;
      }
    }
    for (int i = 0; i < M; i++) {
      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++;
      }
    }
    for (int i = 0; i < M; i++) {
      if (data.at(i).at(0) == 3) {
        if (count3 == 0) {
          third = data.at(i).at(1);
        } else {
          if (data.at(i).at(1) != first) {
            check = 1;
          }
        }
        count3++;
      }
    }
    if (check == 1) {
      cout << -1 << endl;
    } else if (check == 0) {
      if (count == 0) {
        first = 1;
      }
      if (count2 == 0) {
        second = 0;
      }
      if (count3 == 0) {
        third = 0;
      }
      if (N == 3) {
        cout << first << second << third << endl;
      } else {
        cout << first << second << endl;
      }
    }
  }
  return 0;
}
