#include using namespace std; #include using namespace atcoder; using mint=modint998244353; //1000000007; using ll=long long; using pp=pair; #define sr string #define vc vector #define fi first #define se second #define rep(i,n) for(int i=0;i<(int)n;i++) #define pb push_back #define all(v) v.begin(),v.end() #define pque priority_queue #define bpc(a) __builtin_popcount(a) int main(){ int h,w,n;cin>>h>>w>>n; vc v(h+w,vc(0)); mapm; rep(i,n){ int x,y;cin>>x>>y; x--; y--; v[x].pb(h+y); v[h+y].pb(x); m[{x,y+h}]=i; m[{y+h,x}]=i; } vcch(h+w,false); vcans; auto f=[&](auto f,int a,int p)->int{ if(ch[a])return a; ch[a]=true; int res=-1; for(auto au:v[a])if(au!=p){ res=f(f,au,a); if(res==-2)return -2; if(res!=-1)break; } if(res!=-1)ans.pb(a); if(res==a)res=-2; return res; }; rep(i,h)if(!ch[i]){ f(f,i,-1); if(ans.size())break; } if(!ans.size()){ cout<<-1; return 0; } cout<=h){ ans.pb(ans[0]); ans.pb(ans[1]); for(int i=1;i