#include using namespace std; using graph = vector>; vector cycle; void dfs(const graph &to, vector &seen, int v, int p) { seen.at(v) = true; for (auto &&c : to.at(v)) { if (not cycle.empty()) { break; } if (c == p) { continue; } if (seen.at(c)) { cycle.push_back(c); break; } dfs(to, seen, c, v); } if (not cycle.empty()) { cycle.push_back(v); } } int main() { int h, w, n; cin >> h >> w >> n; graph to(h + w + n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; x--; y--; to.at(x).push_back(h + w + i); to.at(h + w + i).push_back(x); to.at(y + h).push_back(h + w + i); to.at(h + w + i).push_back(y + h); } vector seen(h + w + n); for (int i = 0; i < h + w + n; i++) { if (seen.at(i)) { continue; } dfs(to, seen, i, -1); } if (cycle.empty()) { cout << -1 << endl; return 0; } vector ans; int len = cycle.size(), shift = -1; for (int i = 0; i < len; i++) { if (i and cycle.at(i) == cycle.at(0)) { break; } if (cycle.at(i) >= h + w) { ans.push_back(cycle.at(i) - h - w); } else if (shift == -1) { shift = (ans.size() + (cycle.at(i) >= h)) % 2; } } rotate(ans.begin(), ans.begin() + shift, ans.end()); int k = ans.size(); cout << k << endl; for (int i = 0; i < k; i++) { cout << ans.at(i) + 1; if (i < k - 1) { cout << ' '; } else { cout << endl; } } return 0; }