def g;gets.split.map(&:to_i)end l=[*0..n=g[0]] g[0].times{ a,b=g l[a],l[b]=l[b],l[a] } cnt=1 now=l.dup until now==[*0..n] now.map!{|i|l[i]} cnt+=1 end p cnt