#include <bits/stdc++.h>
using namespace std;
long long GCD(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
long long LCM(long long a, long long b) { return a * b / GCD(a, b); }
unsigned GetDigit(unsigned num) { return std::to_string(num).length(); }
int tow(int n) {
  if (n == 0) return 1;
  int x = tow(n / 2);
  x *= x;
  if (n % 2 == 1) x *= 2;
  return x;
}
int a = 0, b = 0, c = 0;
string s = "";
int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> s(m), c(m);
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> c[i];
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == s[j] && i != j) {
        if (c[i] != c[j]) {
          cout << -1 << "\n";
          return 0;
        }
      }
    }
  }
  int ans = 0;
  int d = 0;
  for (int i = 0; i < m; i++) {
    if (s[i] == 3 && a != 0) ans += c[i];
    a = 1;
    if (s[i] == 2 && b != 0) ans += (c[i] * 10);
    b = 1;
    if (s[i] == 1 && d != 0) ans += (c[i] * 100);
    d = 1;
  }
  cout << ans << "\n";
  return 0;
}
