#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int si, ci;
  vector<int> s;
  vector<int> c;
  vector<int> f1(N + 1, 0);
  vector<int> f2(N + 1, 10);
  int i;
  int ans = 0;
  int values[3 + 1] = {0, 100, 10, 1};
  for (i = 1; i <= M; i++) {
    cin >> si >> ci;
    if (f1[si] == 1 and f2[si] > ci) {
      ans -= values[si] * f2[si];
    }
    if (f1[si] == 1 and f2[si] <= ci) {
      continue;
    }
    ans += values[si] * ci;
    f1[si] = 1;
    f2[si] = ci;
    s.push_back(si);
    c.push_back(ci);
  }
  if (N == 3 and ans < 100) {
    cout << "-1" << endl;
    return 0;
  }
  if (N == 2 and ans < 10) {
    cout << "-1" << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}
