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