#include <bits/stdc++.h>
int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> s(M), c(M);
  std::map<int, int> mp;
  for (int i = 0; i < M; i++) {
    std::cin >> s[i] >> c[i];
  }
  for (int i = 1; i <= N; i++) {
    mp[i] = -1;
  }
  for (int i = 0; i < M; i++) {
    if (mp[s[i]] == -1) {
      mp[s[i]] = c[i];
    } else {
      if (mp[s[i]] > c[i]) {
        mp[s[i]] = c[i];
      }
    }
  }
  int ans = 0;
  for (auto p : mp) {
    int keta = p.first;
    int value = p.second;
    if (p.second == -1) {
      continue;
    }
    if (keta == 1)
      keta = 2;
    else if (keta == 2)
      keta = 1;
    else
      keta = 0;
    if (keta == 0) {
      int tmp = value;
      ans += tmp;
    } else {
      int tmp = value;
      for (int i = 0; i < keta; i++) {
        tmp *= 10;
      }
      ans += tmp;
    }
  }
  int tmp_ans = ans;
  int count = 0;
  while (tmp_ans != 0) {
    count++;
    tmp_ans /= 10;
  }
  if (count == N)
    std::cout << ans << std::endl;
  else {
    for (int i = count + 1; i <= N; i++) {
      if (mp[i] == 0) {
        count++;
      }
    }
    if (count == N)
      std::cout << ans << std::endl;
    else
      std::cout << "-1" << std::endl;
  }
}
