#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1001001001;
int main(void) {
  int n(0), m(0), ans(0);
  cin >> n >> m;
  vector<int> v1(n);
  vector<int> v2(m);
  vector<int> dec(3);
  vector<int> dec1(5);
  vector<int> dec2(5);
  vector<int> dec3(5);
  if (m == 0) {
    cout << "-1" << endl;
    return 0;
  }
  for (int i = int(0); i < int(m); ++i) {
    cin >> v1[i] >> v2[i];
  }
  for (int i = int(0); i < int(3); ++i) dec[i] = 10;
  for (int i = int(0); i < int(5); ++i) {
    dec1[i] = 10;
    dec2[i] = 10;
    dec3[i] = 10;
  }
  for (int i = int(0); i < int(m); ++i) {
    if (v1[i] == 1) {
      dec1[i] = v2[i];
    } else if (v1[i] == 2) {
      dec2[i] = v2[i];
    } else if (v1[i] == 3) {
      dec3[i] = v2[i];
    }
  }
  sort(dec1.begin(), dec1.end());
  sort(dec2.begin(), dec2.end());
  sort(dec3.begin(), dec3.end());
  for (int i = int(0); i < int(10); ++i) {
    if (dec1[i] != 0) {
      dec[0] = dec1[i];
      break;
    }
  }
  if (dec[0] == 0) {
    for (int i = int(0); i < int(10); ++i) {
      if (dec2[i] != 0) {
        dec[1] = dec2[i];
        break;
      }
    }
  } else {
    dec[1] = dec2[0];
  }
  if (dec[1] == 0 && dec[0] == 0) {
    for (int i = int(0); i < int(10); ++i) {
      if (dec3[i] != 0) {
        dec[2] = dec3[i];
        break;
      }
    }
  } else {
    dec[2] = dec3[0];
  }
  int n2(0);
  if (n == 1)
    n2 = 2;
  else if (n == 2)
    n2 = 1;
  else
    n2 = 0;
  if (dec[n2] == 10) {
    cout << -1 << endl;
    return 0;
  }
  if (dec[0] != 10) ans += dec[0] * 100;
  if (dec[1] != 10) ans += dec[1] * 10;
  if (dec[2] != 10) ans += dec[2];
  cout << ans;
}
