#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 < M; i++) {
    cin >> S.at(i) >> C.at(i);
  }
  if (N == 1) {
    if (M == 1) {
      cout << C.at(0) << endl;
      return 0;
    } else {
      int sum = 0;
      for (int i = 0; i < (int)(M); i++) {
        sum += C.at(i);
      }
      double heikin = (double)sum / (double)M;
      for (int i = 0; i < (int)(M); i++) {
        if (C.at(i) != heikin) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  } else if (N == 2) {
    int sum1 = 0;
    int sum2 = 0;
    int count1 = 0;
    int count2 = 0;
    double heikin1 = 0;
    double heikin2 = 0;
    for (int i = 0; i < (int)(M); i++) {
      if (S.at(i) == 1) {
        sum1 += C.at(i);
        count1++;
      }
      if (S.at(i) == 2) {
        sum2 += C.at(i);
        count2++;
      }
    }
    if (count1 != 0) heikin1 = (double)sum1 / (double)count1;
    if (count2 != 0) heikin2 = (double)sum2 / (double)count2;
    for (int i = 0; i < (int)(M); i++) {
      if (S.at(i) == 1) {
        if ((double)C.at(i) != heikin1) {
          cout << -1 << endl;
          return 0;
        }
      }
      if (S.at(i) == 2) {
        if ((double)C.at(i) != heikin2) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
    if (heikin1 + heikin2 == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (heikin1 == 0) heikin1 = 1;
    int kotae = (int)heikin1 * 10 + (int)heikin2;
    cout << kotae << endl;
    return 0;
  } else {
    int sum1 = 0;
    int sum2 = 0;
    int sum3 = 0;
    int count1 = 0;
    int count2 = 0;
    int count3 = 0;
    double heikin1 = 0;
    double heikin2 = 0;
    double heikin3 = 0;
    for (int i = 0; i < (int)(M); i++) {
      if (S.at(i) == 1) {
        sum1 += C.at(i);
        count1++;
      }
      if (S.at(i) == 2) {
        sum2 += C.at(i);
        count2++;
      }
      if (S.at(i) == 3) {
        sum3 += C.at(i);
        count3++;
      }
    }
    if (count1 != 0) heikin1 = (double)sum1 / (double)count1;
    if (count2 != 0) heikin2 = (double)sum2 / (double)count2;
    if (count3 != 0) heikin3 = (double)sum3 / (double)count3;
    for (int i = 0; i < (int)(M); i++) {
      if (S.at(i) == 1) {
        if ((double)C.at(i) != heikin1) {
          cout << -1 << endl;
          return 0;
        }
      }
      if (S.at(i) == 2) {
        if ((double)C.at(i) != heikin2) {
          cout << -1 << endl;
          return 0;
        }
      }
      if (S.at(i) == 3) {
        if ((double)C.at(i) != heikin3) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
    if (heikin1 + heikin2 + heikin3 == 0) {
      cout << -1 << endl;
      return 0;
    }
    if (heikin1 == 0) heikin1 = 1;
    int kotae = (int)heikin1 * 100 + (int)heikin2 * 10 + (int)heikin3;
    cout << kotae << endl;
    return 0;
  }
  return 0;
}
