#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(n);
  vector<int> c(n);
  for (long long int i = 0; i < n; i++) cin >> s[i] >> c[i];
  int ans_1 = 10, ans_10 = 10, ans_100 = 10;
  int ans_1_min = 10, ans_10_min = 10, ans_100_min = 10;
  for (int i = 0; i < m; ++i) {
    if (s[i] == 3) {
      ans_1 = c[i];
      if (ans_1 < ans_1_min) ans_1_min = ans_1;
    }
    if (s[i] == 2) {
      ans_10 = c[i];
      if (ans_10 < ans_10_min) ans_10_min = ans_10;
    }
    if (s[i] == 1) {
      ans_100 = c[i];
      if (ans_100 < ans_100_min) ans_100_min = ans_100;
    }
  }
  if (n == 3) {
    if (ans_100 != 10 && ans_1 == 10) ans_1_min = 0;
    if (ans_100 != 10 && ans_10 == 10) ans_10_min = 0;
    if (ans_100 == 10)
      std::cout << -1 << std::endl;
    else
      std::cout << 100 * ans_100_min + 10 * ans_10_min + ans_1_min << std::endl;
  }
  if (n == 2) {
    if (ans_10 != 10 && ans_1 == 10) ans_1_min = 0;
    if (ans_10 == 10)
      std::cout << -1 << std::endl;
    else
      std::cout << 10 * ans_10_min + ans_1_min << std::endl;
  }
  if (n == 1) {
    if (ans_1 == 10)
      std::cout << 0 << std::endl;
    else
      std::cout << ans_1_min << std::endl;
  }
}
