#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m);
  vector<int> c(m);
  for (int i = 0; i < (int)(m); ++i) cin >> s[i] >> c[i];
  bool ans = true;
  int num = 0;
  int min1 = 10;
  int min2 = 10;
  int min3 = 10;
  bool stop1 = true;
  bool stop2 = true;
  for (int i = 0; i < (int)(m); ++i) {
    if (s[i] == 3) {
      if (c[i] < min1 && c[i] != 0)
        min1 = c[i];
      else if (c[i] == 0)
        min1 = 0;
    } else if (s[i] == 2) {
      if (c[i] < min2 && c[i] != 0)
        min2 = c[i];
      else if (c[i] == 0)
        min2 = 0;
    } else if (s[i] == 1) {
      if (c[i] < min3 && c[i] != 0) min3 = c[i];
    }
  }
  for (int i = 0; i < (int)(m); ++i) {
    if (s[i] == 3) {
      int p = 0;
      int q = 0;
      for (int j = 0; j < (int)(m); ++j) {
        if (s[j] == 1 && c[j] != 0) ++p;
      }
      if (p == 0 && n == 3)
        ans = false;
      else {
        for (int j = 0; j < (int)(m); ++j) {
          if (s[j] == 2 && c[j] != 0) ++q;
        }
        if (q == 0 && n == 2) ans = false;
      }
      num += min1;
    } else if (s[i] == 2) {
      int p = 0;
      for (int j = 0; j < (int)(m); ++j) {
        if (s[j] == 3 && c[j] != 0) ++p;
      }
      if (p == 0 && n == 3) ans = false;
      if (stop2) {
        num += min2 * 10;
        stop2 = false;
      }
    } else if (s[i] == 1) {
      if (c[i] == 0 && n == 3) ans = false;
      if (stop1) {
        num += min3 * 100;
        stop1 = false;
      }
    }
  }
  cout << (ans ? num : -1) << endl;
}
