#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int a[5][2];
  for (int i = 0; i < 5; i++) {
    cin >> a[i][0];
    cin >> a[i][1];
  }
  int res[3] = {0, 0, 0};
  for (int j = 0; j < 5; j++) {
    if (res[a[j][0] - 1] != a[j][1] && a[j][1] != 0) {
      res[a[j][0] - 1] = a[j][1];
    }
  }
  if (res[0] != 0 || (res[0] != 0 && res[1] != 0)) {
    cout << res[0] << res[1] << res[2];
  } else {
    cout << "-1";
  }
}
