#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int a = 0;
  int b = 0;
  int d = 0;
  cin >> n >> m;
  vector<int> s(m);
  vector<int> c(n);
  for (long long(i) = (0); (i) < (m * 2); (i)++) {
    cin >> s[i];
    cin >> c[i];
    if (s[i] == 1 && c[i] == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (s[i] == 1) {
      if (a == 0)
        a = 100 * c[i];
      else if (a == 100 * c[i]) {
      } else {
        cout << -1 << endl;
        return 0;
      }
    }
    if (s[i] == 2) {
      if (b == 0)
        b = 10 * c[i];
      else if (b == 10 * c[i]) {
      } else {
        cout << -1 << endl;
        return 0;
      }
    }
    if (s[i] == 3) {
      if (d == 0)
        d = 1 * c[i];
      else if (d == 1 * c[i]) {
      } else {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << a + b + d << endl;
}
