#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int c, s;
  map<int, int> data;
  int flag = false;
  for (int i; i < 3; i++) {
    cin >> s >> c;
    if (data.count(s) && data.at(s) != c) {
      flag = true;
      cout << "-1" << endl;
      return 0;
    } else
      data[s] = c;
  }
  int n;
  if (data.count(1)) {
    if (data.at(1) != 0)
      n += data.at(1) * 100;
    else
      flag = true;
  }
  if (data.count(2)) {
    if (data.at(2) != 0)
      n += data.at(2) * 10;
    else
      flag = true;
  }
  if (data.count(3)) {
    if (data.count(3)) n += data.at(3);
  }
  if (flag) {
    cout << "-1";
  } else
    cout << n;
  cout << endl;
  return 0;
}
