#include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; vector g[200000]; bool used[200000]; int pre[200000]; vector ans; void make_loop(int v, int end){ while(v != end){ ans.push_back(v); v = pre[v]; } ans.push_back(end); } bool dfs(int v){ used[v] = true; for(int to: g[v]){ if(!used[to]){ pre[to] = v; if(dfs(to)) return true; }else if(pre[v] != to){ make_loop(v, to); return true; } } return false; } using P = pair; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int h, w; cin >> h >> w; int n; cin >> n; map idx; vector x(n), y(n); for(int i = 0; i < n; i++){ cin >> x[i] >> y[i]; x[i]--; y[i]--; g[x[i]].push_back(h+y[i]); g[h+y[i]].push_back(x[i]); idx[P(x[i], h+y[i])] = i; idx[P(h+y[i], x[i])] = i; } for(int i = 0; i < h+w; i++){ if(!used[i]){ if(dfs(i)){ int m = ans.size(); cout << m << endl; vector v(m); for(int j = 0; j < m; j++){ int k = (j+1)%m; v[j] = idx[P(ans[j], ans[k])]; } if(x[v[0]] != x[v[1]]){ for(int j = 0; j < m; j++) cout << v[j]+1 << ' '; cout << endl; }else{ for(int j = 0; j < m; j++) cout << v[(j+1)%m]+1 << ' '; cout << endl; } // for(int j: v) cout << x[j] << ' ' << y[j] << endl; return 0; } } } cout << -1 << endl; }