#include <bits/stdc++.h>
using namespace std;
int mp1[20][20], mp2[20][20];
int de1[20], de2[20];
set<int> ans;
int main() {
  int x, y;
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> x >> y, mp1[x][y] = mp1[y][x] = 1, de1[x]++, de1[y]++;
  for (int i = 1; i <= m; i++)
    cin >> x >> y, mp2[x][y] = mp2[y][x] = 1, de2[x]++, de2[y]++;
  bool ok = 1;
  for (int i = 1; i <= 9; i++)
    for (int j = 1; j <= 9; j++)
      if (mp1[i][j]) {
        if ((de2[i] - mp2[i][j]) && (de2[j] - mp2[i][j]))
          return cout << -1, 0;
        else if ((de2[i] - mp2[i][j]))
          ans.insert(i);
        else if ((de2[j] - mp2[i][j]))
          ans.insert(j);
      }
  for (int i = 1; i <= 9; i++)
    for (int j = 1; j <= 9; j++)
      if (mp2[i][j]) {
        if ((de1[i] - mp1[i][j]) && (de1[j] - mp1[i][j]))
          return cout << -1, 0;
        else if ((de1[i] - mp1[i][j]))
          ans.insert(i);
        else if ((de1[j] - mp1[i][j]))
          ans.insert(j);
      }
  if (ans.size() > 1)
    cout << 0 << endl;
  else if (ans.size())
    cout << *ans.begin() << endl;
  else
    cout << -1 << endl;
  return 0;
}
