#include <bits/stdc++.h>
using namespace std;
struct S {
  int x, y;
} a[1000], b[1000];
int vis[1000], p1[1000], p2[1000];
int main() {
  int n, m;
  cin >> n >> m;
  for (register int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;
  for (register int i = 1; i <= m; i++) cin >> b[i].x >> b[i].y;
  for (register int i = 1; i <= n; i++) {
    vis[a[i].x] = vis[a[i].y] = 1;
    for (register int j = 1; j <= m; j++)
      if (vis[b[j].x] + vis[b[j].y] == 1) {
        if (vis[b[j].x] == 1) {
          if (!p1[i])
            p1[i] = b[j].x;
          else if (p1[i] != b[j].x)
            return !printf("-1");
        } else {
          if (!p1[i])
            p1[i] = b[j].y;
          else if (p1[i] != b[j].y)
            return !printf("-1");
        }
      }
    vis[a[i].x] = vis[a[i].y] = 0;
  }
  for (register int i = 1; i <= m; i++) {
    vis[b[i].x] = vis[b[i].y] = 1;
    for (register int j = 1; j <= n; j++)
      if (vis[a[j].x] + vis[a[j].y] == 1) {
        if (vis[a[j].x] == 1) {
          if (!p2[i])
            p2[i] = a[j].x;
          else if (p2[i] != a[j].x)
            return !printf("-1");
        } else {
          if (!p2[i])
            p2[i] = a[j].y;
          else if (p2[i] != a[j].y)
            return !printf("-1");
        }
      }
    vis[b[i].x] = vis[b[i].y] = 0;
  }
  int ans = 0;
  for (register int i = 1; i <= n; i++)
    if (p1[i]) {
      if (!ans)
        ans = p1[i];
      else if (ans != p1[i])
        return !printf("0");
    }
  cout << ans;
  return 0;
}
