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