#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x = 0;
  cin >> n >> m;
  vector<int> ket(m);
  vector<int> suu(m);
  for (int i = 0; i < m; i++) cin >> ket[i] >> suu[i];
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (i == j) continue;
      if (ket[i] == ket[j] && suu[i] != suu[j]) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (ket[i] == 1 && suu[i] == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 1; i < m + 1; i++) {
    for (int j = 0; j < m + 1; j++) {
      if (i == ket[j]) {
        x += pow(n - i, 10) * suu[j];
        break;
      }
    }
  }
  cout << x << endl;
}
