#include <bits/stdc++.h>
using namespace std;
int main() {
  int left_ans = 0;
  int mid_ans = 0;
  int right_ans = 0;
  int n, m;
  cin >> n >> m;
  int s[m], c[m];
  for (int i = 0; i < (m); ++i) cin >> s[i] >> c[i];
  for (int i = 0; i < (m); ++i) {
    if (n == 3) {
      if (s[i] == 1) {
        if (left_ans == 0 || left_ans == c[i])
          left_ans = c[i];
        else {
          cout << -1 << endl;
          return 0;
        }
      } else if (s[i] == 2) {
        if (mid_ans == 0 || mid_ans == c[i])
          mid_ans = c[i];
        else {
          cout << -1 << endl;
          return 0;
        }
      } else if (s[i] == 3) {
        if (right_ans == 0 || right_ans == c[i])
          right_ans = c[i];
        else {
          cout << -1 << endl;
          return 0;
        }
      }
    } else if (n == 2) {
      if (s[i] == 1) {
        if (mid_ans == 0 || mid_ans == c[i])
          mid_ans = c[i];
        else {
          cout << -1 << endl;
          return 0;
        }
      } else if (s[i] == 2) {
        if (right_ans == 0 || right_ans == c[i])
          right_ans = c[i];
        else {
          cout << -1 << endl;
          return 0;
        }
      }
    } else {
      if (right_ans == 0 || right_ans == c[i])
        right_ans = c[i];
      else {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  if (n == 3) {
    if (left_ans == 0) {
      cout << -1 << endl;
      return 0;
    } else
      cout << left_ans << mid_ans << right_ans << endl;
  } else if (n == 2) {
    if (mid_ans == 0) {
      cout << -1 << endl;
      return 0;
    } else
      cout << mid_ans << right_ans << endl;
  } else {
    cout << right_ans << endl;
  }
  return 0;
}
