#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(m);
  vector<int> c(m);
  vector<int> cnt(3), cnt2(3);
  for (int i = 0; i < (3); i++) cnt[i] = -101, cnt2[i] = 0;
  int ans = 0;
  if (m == 0) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < (m); i++) {
    cin >> s[i] >> c[i];
    if (n == 1) {
      if (s[i] > 1) {
        cout << -1 << endl;
        return 0;
      } else {
        if (cnt[0] != c[i] && cnt2[0] != 0) {
          cout << -1 << endl;
          return 0;
        }
        if (cnt2[0] == 0) {
          ans += c[i];
        }
        cnt2[0]++;
        cnt[0] = c[i];
      }
    } else if (n == 2) {
      if (s[i] > 2) {
        cout << -1 << endl;
        return 0;
      } else {
        if (s[i] == 2) {
          if (cnt[1] != c[i] && cnt2[1] != 0) {
            cout << -1 << endl;
            return 0;
          }
          if (cnt2[1] == 0) {
            ans += c[i];
          }
          cnt2[1]++;
          cnt[1] = c[i];
        } else if (s[i] == 1) {
          if (cnt[0] != c[i] && cnt2[0] != 0) {
            cout << -1 << endl;
            return 0;
          }
          if (c[i] == 0) {
            cout << -1 << endl;
            return 0;
          }
          if (cnt2[0] == 0) {
            ans += c[i] * 10;
          }
          cnt2[0]++;
          cnt[0] = c[i];
        }
      }
    } else {
      if (s[i] == 3) {
        if (cnt[2] != c[i] && cnt2[2] != 0) {
          cout << -1 << endl;
          return 0;
        }
        if (cnt2[2] == 0) {
          ans += c[i];
        }
        cnt2[2]++;
        cnt[2] = c[i];
      }
      if (s[i] == 2) {
        if (cnt[1] != c[i] && cnt2[1] != 0) {
          cout << -1 << endl;
          return 0;
        }
        if (cnt2[1] == 0) {
          ans += c[i] * 10;
        }
        cnt2[1]++;
        cnt[1] = c[i];
      } else if (s[i] == 1) {
        if (cnt[0] != c[i] && cnt2[0] != 0) {
          cout << -1 << endl;
          return 0;
        }
        if (c[i] == 0) {
          cout << -1 << endl;
          return 0;
        }
        if (cnt2[0] == 0) {
          ans += c[i] * 100;
        }
        cnt2[0]++;
        cnt[0] = c[i];
      }
    }
  }
  cout << ans << endl;
  return 0;
}
