#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long double scal(long double x, long double y, long double x1, long double y1) {
  return x * y1 - x1 * y;
}
int n, m;
int a[20][2], b[20][2];
int aa[20][10], bb[20][10];
int p[10];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1];
  for (int i = 0; i < m; i++) cin >> b[i][0] >> b[i][1];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if ((a[i][0] == b[j][0] && a[i][1] == b[j][1]) ||
          (a[i][1] == b[j][0] && a[i][0] == b[j][1]))
        continue;
      if (a[i][0] == b[j][0]) {
        aa[i][a[i][0]] = 1;
        bb[j][a[i][0]] = 1;
      }
      if (a[i][0] == b[j][1]) {
        aa[i][a[i][0]] = 1;
        bb[j][a[i][0]] = 1;
      }
      if (a[i][1] == b[j][1]) {
        aa[i][a[i][1]] = 1;
        bb[j][a[i][1]] = 1;
      }
      if (a[i][1] == b[j][0]) {
        aa[i][a[i][1]] = 1;
        bb[j][a[i][1]] = 1;
      }
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 1; j <= 9; j++)
      if (aa[i][j] == 1) p[j] = 1;
  int anss = 0;
  for (int i = 1; i <= 9; i++) anss += p[i];
  if (anss == 1) {
    for (int i = 1; i <= 9; i++)
      if (p[i]) cout << i;
    return 0;
  }
  int ans1 = 1, ans2 = 1;
  for (int i = 0; i < n; i++) {
    int t = 0;
    for (int j = 1; j <= 9; j++) t += aa[i][j];
    if (t > 1) ans1 = 0;
  }
  for (int i = 0; i < m; i++) {
    int t = 0;
    for (int j = 1; j <= 9; j++) t += bb[i][j];
    if (t > 1) ans2 = 0;
  }
  if (ans1 && ans2)
    cout << 0;
  else
    cout << -1;
}
