ll@N,@M,@A[M],@T[N][M],a[N]{},b[N]{},s=N,t;while(s)rep(i,N)if(!b[i]){if(A[t=T[i][a[i]]])(A[t],s,b[i])--;else a[i]++;}rep(i,N)wtSp(T[i][a[i]]);