#include <bits/stdc++.h>
using namespace std;
int g[15][15];
const int INF = 1001001001;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
vector<pair<long long, int>> factorize(long long n) {
  vector<pair<long long, int>> res;
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i) continue;
    res.emplace_back(i, 0);
    while (n % i == 0) {
      n /= i;
      res.back().second++;
    }
  }
  if (n != 1) res.emplace_back(n, 1);
  return res;
}
int dp[1010][1010];
int bingo[3][3];
bool flag[3][3];
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m), c(m);
  for (int i = 0; i < m; ++i) {
    cin >> s[i] >> c[i];
  }
  int ans = 0;
  bool flag = true;
  vector<bool> keta(n, false);
  vector<int> kazu(n);
  for (int i = 0; i < m; ++i) {
    if (keta[s[i]] == true && kazu[s[i]] == c[i])
      continue;
    else if (keta[s[i]] == true && kazu[s[i]] != c[i]) {
      flag = false;
    } else if (keta[s[i]] == false) {
      keta[s[i]] = true;
      kazu[s[i]] = c[i];
      if (s[i] == 3)
        ans += c[i];
      else if (s[i] == 2)
        ans += c[i] * 10;
      else
        ans += c[i] * 100;
    }
  }
  int kurai = 0;
  int ans2 = ans;
  while (ans2 > 0) {
    ans2 /= 10;
    kurai++;
  }
  if (flag == false) cout << -1 << endl;
  if (flag == true && kurai == n)
    cout << ans << endl;
  else if (flag == true && kurai != n)
    cout << -1 << endl;
  else if (flag == true && ans == 0 && n == 1)
    cout << ans << endl;
  return 0;
}
