#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, s[100], c[100];
  cin >> n >> m;
  for (int i = 0; i < m; i++) cin >> s[i] >> c[i];
  int minX = 10, minY = 10, minZ = 10;
  for (int i = 0; i < m; i++) {
    if (s[i] == 1 and c[i] != 0) minX = min(minX, c[i]);
    if (s[i] == 2) minY = min(minY, c[i]);
    if (s[i] == 3) minZ = min(minZ, c[i]);
  }
  if (n == 1) {
    int minX1 = 10;
    for (int i = 0; i < m; i++) {
      if (s[i] == 1) minX1 = min(minX1, c[i]);
    }
    cout << ((minX1 < 10) ? minX1 : 0) << endl;
  } else if (n >= 2) {
    if (minX < 10) {
      cout << minX;
      cout << ((minY < 10) ? minY : 0);
      if (n == 3) cout << ((minZ < 10) ? minZ : 0);
    } else
      cout << -1;
    cout << endl;
  }
  return 0;
}
