#include <bits/stdc++.h>
using namespace std;
inline void normal(long long &a) {
  a %= 1000000007;
  (a < 0) && (a += 1000000007);
}
inline long long modMul(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline long long modPow(long long b, long long p) {
  long long r = 1;
  while (p) {
    if (p & 1) r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline long long modInverse(long long a) { return modPow(a, 1000000007 - 2); }
inline long long modDiv(long long a, long long b) {
  return modMul(a, modInverse(b));
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > a(m);
  for (int i = 0; i < (int)(m); i++) {
    cin >> a[i].first >> a[i].second;
  }
  sort((a).begin(), (a).end());
  if (m == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < (int)(m - 1); i++) {
    if (a[i].first == a[i + 1].first && a[i].second != a[i + 1].second) {
      cout << -1 << endl;
      return 0;
    }
  }
  int ans1 = 0, ans2 = 0, ans3 = 0;
  for (int i = 0; i < (int)(m); i++) {
    if (a[i].first == 1) {
      ans1 = a[i].second * 100;
    }
    if (a[i].first == 2) {
      ans2 = a[i].second * 10;
    }
    if (a[i].first == 3) {
      ans3 = a[i].second;
    }
  }
  cout << ans1 + ans2 + ans3 << endl;
}
