#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  long long n, k, ans = 0, ans1 = 0;
  cin >> n >> k;
  long long s[k], c[k];
  for (int i = 0; i < k; i++) {
    cin >> s[i] >> c[i];
  }
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < k; j++) {
      if (i == j) {
        if (c[j] == 0 && s[j] != 3 && s[j] != 0) {
          cout << "-1" << endl;
          ;
          return 0;
        }
      } else if (s[i] == s[j] && c[i] != c[j]) {
        cout << "-1" << endl;
        ;
        return 0;
      } else if (c[j] == 0 && s[j] != 3 && s[j] != 0) {
        cout << "-1" << endl;
        ;
        return 0;
      } else if (s[i] == s[j] && c[i] == c[j]) {
        s[i] = 0;
        c[i] = 0;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    if (s[i] == 1) {
      ans += 100 * c[i];
    } else if (s[i] == 2) {
      ans += 10 * c[i];
    } else {
      ans += c[i];
    }
  }
  cout << ans << endl;
  ;
}
