#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int s[M], c[M];
  for (int i = 0; i < M; i++) cin >> s[i] >> c[i];
  int result_100 = 10, result_10 = 10, result_1 = 10;
  int ans;
  if (N == 3) {
    for (int i = 0; i < N; i++) {
      if (s[i] == 1 && result_100 > c[i]) {
        result_100 = c[i];
      }
      if (s[i] == 2 && result_10 > c[i]) {
        result_10 = c[i];
      }
      if (s[i] == 3 && result_1 > c[i]) {
        result_1 = c[i];
      }
    }
    if (result_10 == 10) {
      result_10 = 0;
    }
    if (result_1 == 10) {
      result_1 = 0;
    }
    ans = result_100 * 100 + result_10 * 10 + result_1;
    if (result_100 == 10 | result_100 == 0) {
      ans = -1;
    }
  }
  if (N == 2) {
    for (int i = 0; i < N; i++) {
      if (s[i] == 2 && result_10 > c[i]) {
        result_10 = c[i];
      }
      if (s[i] == 3 && result_1 > c[i]) {
        result_1 = c[i];
      }
    }
    ans = result_10 * 10 + result_1;
    if (result_10 == 10 | result_10 == 0) {
      ans = -1;
    }
  }
  if (N == 1) {
    for (int i = 0; i < N; i++) {
      if (s[i] == 3 && result_1 > c[i]) {
        result_1 = c[i];
      }
    }
    ans = result_1;
    if (result_1 == 10 | result_1 == 0) {
      ans = -1;
    }
  }
  cout << ans << endl;
}
