#include #include using namespace std; using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) {a = max(a, b);} template void chmin(T& a, const T& b) {a = min(a, b);} using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w, n; cin >> h >> w >> n; vector> to(h + w); rep(i, n) { int x, y; cin >> x >> y; x--, y--; to[x].emplace_back(h + y, i); to[h + y].emplace_back(x, i); } vector visited(h + w); vector

path; vector used(n); auto dfs = [&](auto&& self, int u, int p=-1) -> void { if (visited[u]) { VI ans; ans.push_back(p); while(path.back().first != u) { ans.push_back(path.back().second); path.pop_back(); } if (u >= h) { rotate(ans.begin(), ans.begin() + 1, ans.end()); } int sz = ans.size(); cout << sz << '\n'; rep(i, sz) cout << ans[i] + 1 << " \n"[i + 1 == sz]; exit(0); } visited[u] = true; path.emplace_back(u, p); for(auto [v, eid]: to[u]) if (!used[eid]) { used[eid] = true; self(self, v, eid); } path.pop_back(); }; rep(i, h) if (!visited[i]) dfs(dfs, i); cout << -1 << endl; }