#include <bits/stdc++.h>
using namespace std;
int n, m;
int s[5], c[5];
int z[3] = {10, 10, 10};
int main() {
  int res = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) cin >> s[i] >> c[i];
  for (int i = 0; i < m; i++) {
    z[s[i] - 1] = min(c[i], z[s[i] - 1]);
  }
  if (n == 1 && (z[0] != 10 || z[1] != 10)) {
    cout << "-1" << endl;
    return 0;
  } else if (n == 2 && (z[0] != 10 || z[1] != 10)) {
    cout << "-1" << endl;
    return 0;
  } else if (n == 3 && (z[0] == 10)) {
    cout << "-1" << endl;
    return 0;
  }
  for (int i = 100, j = 0; i > 0; i /= 10) {
    if (z[j] < 10)
      res += i * z[j];
    else if (n - 1 == j)
      res += i * 1;
    j++;
  }
  if (z[n] == 10) {
    cout << "-1" << endl;
    return 0;
  }
  cout << res << endl;
  return 0;
}
