#include <bits/stdc++.h>
using namespace std;
int n, m, ha1[50][50], ha2[50][50], ans1[50][50], ans2[50][50], x = 0, flag = 0;
int main() {
  memset(ha1, 0, sizeof(ha1));
  memset(ha2, 0, sizeof(ha2));
  memset(ans1, 0, sizeof(ans1));
  memset(ans2, 0, sizeof(ans2));
  cin >> n >> m;
  for (int i = 1, xx, yy; i <= n; ++i) {
    cin >> xx >> yy;
    ha1[xx][yy] = 1;
    ha1[yy][xx] = 1;
  }
  for (int i = 1, xx, yy; i <= m; ++i) {
    cin >> xx >> yy;
    ha2[xx][yy] = 1;
    ha2[yy][xx] = 1;
  }
  for (int i = 1; i <= 9; ++i)
    for (int j = 1; j <= 9; ++j) {
      if (i == j) continue;
      for (int k = 1; k <= 9; ++k) {
        if (k == i || k == j) continue;
        if (ha1[i][j] == ha2[i][k] && ha1[i][j]) {
          ++ans1[i][j];
          ++ans2[i][k];
          x = i;
        }
      }
    }
  for (int i = 1; i <= 9; ++i)
    for (int j = 1; j <= 9; ++j)
      if (ans1[i][j]) {
        flag++;
        break;
      }
  if (flag == 1) {
    cout << x << endl;
    return 0;
  }
  x = 0;
  for (int i = 1; i <= 9; ++i)
    for (int j = 1; j <= 9; ++j) {
      if (i == j) continue;
      if ((ans1[i][j] && ans1[j][i]) || (ans2[i][j] && ans2[j][i])) x = -1;
    }
  cout << x << endl;
  return 0;
}
