#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, z;
long long a[111][3], b[111][3];
long long za[111][11], zb[111][11], f[111];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (i = 1; i <= n; i++) cin >> a[i][1] >> a[i][2];
  for (i = 1; i <= m; i++) cin >> b[i][1] >> b[i][2];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      if ((a[i][1] == b[j][1] || a[i][1] == b[j][2]) && a[i][2] != b[j][1] &&
          a[i][2] != b[j][2]) {
        za[i][1] = 1;
        if (a[i][1] == b[j][1])
          zb[j][1] = 1;
        else
          zb[j][2] = 1;
        f[a[i][1]]++;
      }
      if ((a[i][2] == b[j][1] || a[i][2] == b[j][2]) && a[i][1] != b[j][1] &&
          a[i][1] != b[j][2]) {
        za[i][2] = 1;
        if (a[i][2] == b[j][1])
          zb[j][1] = 1;
        else
          zb[j][2] = 1;
        f[a[i][2]]++;
      }
    }
  for (i = 1; i <= 9; i++)
    if (f[i] != 0) {
      if (z == 0)
        z = i;
      else {
        z = -1;
        break;
      }
    }
  if (z != -1) {
    cout << z;
    return 0;
  }
  z = 0;
  for (i = 1; i <= n; i++)
    if (za[i][1] == 1 && za[i][2] == 1) z = -1;
  for (i = 1; i <= m; i++)
    if (zb[i][1] == 1 && zb[i][2] == 1) z = -1;
  cout << z;
}
