#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, ans = 0;
  cin >> n >> m;
  int s[m], c[m];
  for (int i = 0; i < (int)(m); ++i) cin >> s[i] >> c[i];
  bool flag = true;
  for (int i = 0; i < (int)(m); ++i) {
    for (int j = i + 1; j < (int)(m); ++j) {
      if (s[i] == s[j] and c[i] != c[j]) {
        flag = false;
        ans = -1;
      } else if (s[i] == s[j] and c[i] == c[j])
        c[j] = 0;
    }
  }
  if (flag) {
    for (int i = 0; i < (int)(m); ++i)
      if (s[i] == 1)
        ans += c[i] * 100;
      else if (s[i] == 2)
        ans += c[i] * 10;
      else if (s[i] == 3)
        ans += c[i];
  }
  if (ans == 0 and n != 1) ans = -1;
  cout << ans << "\n";
}
