#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m), c(m);
  for (int i = 0; i < (int)(m); i++) cin >> s.at(i) >> c.at(i);
  if (n == 1) {
    int ans = 10;
    for (int i = 0; i < (int)(m); i++) {
      ans = min(ans, c.at(i));
    }
    if (ans == 10) {
      cout << "-1" << endl;
      return 0;
    }
    cout << ans << endl;
    return 0;
  } else if (n == 2) {
    int keta1 = 10;
    int keta2 = 10;
    for (int i = 0; i < (int)(m); i++) {
      if (s.at(i) == 1 && c.at(i) != 0)
        keta1 = min(keta1, c.at(i));
      else if (s.at(i) == 2)
        keta2 = min(keta2, c.at(i));
    }
    if (keta1 == 10) {
      cout << "-1" << endl;
      return 0;
    }
    if (keta2 == 10) keta2 = 0;
    cout << keta1 * 10 + keta2 << endl;
    return 0;
  } else if (n == 3) {
    int keta1 = 10;
    int keta2 = 10;
    int keta3 = 10;
    for (int i = 0; i < (int)(m); i++) {
      if (s.at(i) == 1 && c.at(i) != 0)
        keta1 = min(keta1, c.at(i));
      else if (s.at(i) == 2)
        keta2 = min(keta2, c.at(i));
      else if (s.at(i) == 3)
        keta3 = min(keta3, c.at(i));
    }
    if (keta1 == 10) {
      cout << "-1" << endl;
      return 0;
    }
    if (keta2 == 10) keta2 = 0;
    if (keta3 == 10) keta3 = 0;
    cout << keta1 * 100 + keta2 * 10 + keta3 << endl;
    return 0;
  }
  return 0;
}
