#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int num_1 = 9;
  int num_2 = 9;
  int num_3 = 9;
  bool cout_1 = false;
  bool cout_2 = false;
  bool cout_3 = false;
  for (int i = 0; i < (int)(M); i++) {
    int s, c;
    cin >> s >> c;
    if (s == 1) {
      num_1 = min(num_1, c);
      cout_1 = true;
    } else if (s == 2) {
      num_2 = min(num_2, c);
      cout_2 = true;
    } else {
      num_3 = min(num_3, c);
      cout_3 = true;
    }
  }
  if (cout_2 == false) num_2 = 0;
  if (cout_3 == false) num_3 = 0;
  int ans = 0;
  if (cout_1 == true && num_1 != 0) {
    ans = 100 * num_1 + 10 * num_2 + num_3;
    cout << ans << endl;
  } else
    cout << -1 << endl;
}
