#include <bits/stdc++.h>
using namespace std;
template <unsigned int MD>
struct ModInt {
  using M = ModInt;
  const static M G;
  unsigned int v;
  ModInt(long long _v = 0) { set_v(unsigned int(_v % MD + MD)); }
  M& set_v(unsigned int _v) {
    v = (_v < MD) ? _v : _v - MD;
    return *this;
  }
  explicit operator bool() const { return v != 0; }
  M operator-() const { return M() - *this; }
  M operator+(const M& r) const { return M().set_v(v + r.v); }
  M operator-(const M& r) const { return M().set_v(v + MD - r.v); }
  M operator*(const M& r) const {
    return M().set_v(unsigned int(unsigned long long(v) * r.v % MD));
  }
  M operator/(const M& r) const { return *this * r.inv(); }
  M& operator+=(const M& r) { return *this = *this + r; }
  M& operator-=(const M& r) { return *this = *this - r; }
  M& operator*=(const M& r) { return *this = *this * r; }
  M& operator/=(const M& r) { return *this = *this / r; }
  bool operator==(const M& r) const { return v == r.v; }
  M pow(long long n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1) r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const { return pow(MD - 2); }
  friend ostream& operator<<(ostream& os, const M& r) { return os << r.v; }
};
using Mint = ModInt<1000000007>;
const double PI = 3.14159265358979323846;
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
bool is_prime(long long N) {
  if (N == 1) return false;
  for (long long i = 2; i * i <= N; ++i) {
    if (N % i == 0) return false;
  }
  return true;
}
int main() {
  long long n, m;
  cin >> n >> m;
  vector<pair<long long, long long>> p(m);
  long long ans = 0;
  bool ok1 = true;
  bool ok2 = true;
  bool ok3 = true;
  for (long long i = (long long)0; i < (long long)m; i++) {
    long long a, b;
    cin >> a >> b;
    p[i] = make_pair(a, b);
  }
  sort(p.begin(), p.end());
  for (long long i = (long long)0; i < (long long)m; i++) {
    if (p[i].first == 1 && ok1) {
      ans += 100 * p[i].second;
      ok1 = false;
    }
    if (p[i].first == 2 && ok2) {
      ans += 10 * p[i].second;
      ok2 = false;
    }
    if (p[i].first == 3 && ok3) {
      ans += p[i].second;
      ok3 = false;
    }
  }
  for (long long i = (long long)0; i < (long long)m - 1; i++) {
    if (p[i].first == p[i + 1].first) {
      if (p[i].second != p[i + 1].second) {
        ans = -1;
        break;
      }
    }
  }
  if (p[0].first == 1 && p[0].second == 0) ans = -1;
  cout << ans << endl;
}
