#include #include #include #include #include using namespace std; using i64 = int64_t; using u64 = uint64_t; #define rep(i,n) for(int i=0; i<(n); i++) struct Edge{ int to; int i; }; int H,W,N; vector> E; int main() { cin >> H >> W >> N; E.resize(H+W); rep(i,N){ int y,x; cin >> y >> x; y--; x--; E[y].push_back({H+x,i}); E[H+x].push_back({y,i}); } vector D(H+W,-1); vector P(H+W,-1); vector Pi(H+W,-1); vector ans; for(int s=0; s<=H; s++) if(D[s] == -1){ P[s] = -2; D[s] = 0; vector I; I.push_back(s); rep(i,I.size()){ int p = I[i]; for(auto e : E[p]){ if(D[e.to] == -1){ D[e.to] = D[p] + 1; P[e.to] = p; Pi[e.to] = e.i; I.push_back(e.to); } if(D[p] != 0) if(D[e.to] == D[p] + 1) if(P[e.to] != p){ vector ans1 = {e.i}; int x1 = p; vector ans2 = {Pi[e.to]}; int x2 = P[e.to]; while(x1 != x2){ ans1.push_back(Pi[x1]); ans2.push_back(Pi[x2]); x1 = P[x1]; x2 = P[x2]; } if(D[x1] % 2 == 1){ ans1.push_back(ans2.back()); ans2.pop_back(); } while(ans1.size()){ ans.push_back(ans1.back()); ans1.pop_back(); } for(auto a : ans2) ans.push_back(a); cout << ans.size() << "\n"; rep(i,ans.size()){ if(i) cout << " "; cout << (ans[i]+1); } cout << "\n"; return 0; } } } for(int p : I) D[p] = -2; } cout << "-1\n"; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;