#include <bits/stdc++.h>
const int maxn = 100001;
using namespace std;
int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
int rows, coloums;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int pos1 = 10;
  int pos2 = 10;
  int pos3 = 10;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int second, c;
    cin >> second >> c;
    if (second == 1 && c != 0) {
      pos1 = min(pos1, c);
    } else if (second == 2) {
      pos2 = min(pos2, c);
    } else if (second == 3) {
      pos3 = min(pos3, c);
    }
  }
  if (pos1 != 10) {
    if (pos3 == 10) pos3 = 0;
    if (pos2 == 10) pos2 = 0;
    if (n == 1) {
      cout << pos1 << endl;
    } else if (n == 2) {
      cout << pos1 << pos2 << endl;
    } else {
      cout << pos1 << pos2 << pos3 << endl;
    }
  } else {
    cout << -1 << endl;
  }
}
