#include <bits/stdc++.h>
using namespace std;
pair<int, int> v1[20], v2[20];
bool vec1[20][10], vec2[20][10], ras1[10], ras2[10];
int main() {
  int max1(-1), max2(-1), n, m, ans1(0), ans2(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> v1[i].first >> v1[i].second;
  }
  for (int i = 1; i <= m; ++i) {
    cin >> v2[i].first >> v2[i].second;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (((v1[i].first == v2[j].first) + (v1[i].first == v2[j].second) +
           (v1[i].second == v2[j].first) + (v1[i].second == v2[j].second)) ==
          1) {
        if (v1[i].first == v2[j].first) {
          vec1[i][v1[i].first] = true;
        }
        if (v1[i].first == v2[j].second) {
          vec1[i][v1[i].first] = true;
        }
        if (v1[i].second == v2[j].first) {
          vec1[i][v1[i].second] = true;
        }
        if (v1[i].second == v2[j].second) {
          vec1[i][v1[i].second] = true;
        }
      }
    }
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (((v2[i].first == v1[j].first) + (v2[i].first == v1[j].second) +
           (v2[i].second == v1[j].first) + (v2[i].second == v1[j].second)) ==
          1) {
        if (v2[i].first == v1[j].first) {
          vec2[i][v2[i].first] = true;
        }
        if (v2[i].first == v1[j].second) {
          vec2[i][v2[i].first] = true;
        }
        if (v2[i].second == v1[j].first) {
          vec2[i][v2[i].second] = true;
        }
        if (v2[i].second == v1[j].second) {
          vec2[i][v2[i].second] = true;
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    ras1[v1[i].first] |= vec1[i][v1[i].first];
    ras1[v1[i].second] |= vec1[i][v1[i].second];
    ans1 = 0;
    for (int j = 1; j <= 9; ++j) {
      ans1 += ras1[j];
    }
    max1 = max(max1, vec1[i][v1[i].first] + vec1[i][v1[i].second]);
  }
  for (int i = 1; i <= m; ++i) {
    ras2[v2[i].first] |= vec2[i][v2[i].first];
    ras2[v2[i].second] |= vec2[i][v2[i].second];
    ans2 = 0;
    for (int j = 1; j <= 9; ++j) {
      ans2 += ras2[j];
    }
    max2 = max(max2, vec2[i][v2[i].first] + vec2[i][v2[i].second]);
  }
  if (ans1 == 1) {
    for (int i = 1; i <= n; ++i) {
      if ((int)vec1[i][v1[i].first] + (int)vec1[i][v1[i].second] == 1) {
        if (vec1[i][v1[i].first]) {
          cout << v1[i].first;
          return 0;
        }
        cout << v1[i].second;
        return 0;
      }
    }
  } else if (ans2 == 1) {
    for (int i = 1; i <= m; ++i) {
      if ((int)vec2[i][v2[i].first] + (int)vec2[i][v2[i].second] == 1) {
        if (vec2[i][v2[i].first]) {
          cout << v2[i].first;
          return 0;
        }
        cout << v2[i].second;
        return 0;
      }
    }
  } else {
    if (max1 == 1 && max2 == 1) {
      cout << 0;
      return 0;
    }
    cout << -1;
    return 0;
  }
  return 0;
}
