#include <bits/stdc++.h>
using namespace std;
long long INF = 1e18;
int main() {
  int n, m;
  cin >> n >> m;
  long long ans = 0;
  int s[m + 100], c[m + 100];
  int flag = 0;
  int w[m + 100];
  for (int i = 0; i < m; i++) {
    cin >> s[i] >> c[i];
    if (s[i] > n) {
      cout << -1 << endl;
      return 0;
    }
    if (s[i] == n && c[i] != 0 || (n == 1)) {
      flag = 1;
    }
  }
  int box;
  for (int i = 0; i < m; i++) {
    box = s[i];
    for (int j = i + 1; j < m; j++) {
      if (box == s[j] && c[i] != c[j]) {
        cout << -1 << endl;
        return 0;
      }
      if (box == s[j] && c[i] == c[j]) {
        c[j] = 0;
      }
    }
  }
  if (flag == 0) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    if (s[i] == 1 && n == 3) {
      ans += c[i] * 100;
    } else if ((s[i] == 2 && n == 3) || (s[i] == 1 && n == 2)) {
      ans += c[i] * 10;
    } else {
      ans += c[i];
    }
  }
  box = ans;
  if (n == 2 && ans / 10 <= 0) {
    cout << -1 << endl;
    return 0;
  } else if (n == 3 && ans / 100 <= 0) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}
