s//123 456 789 z147,258,369,159,357/;$p=X;sub d{/z/;print$`.$p;@_&&die@_} {d;1until<>=~/\d/&&s/$&/$p/g;/$p{3}/?d$p:/\d/?$p^=v23:d Tie;redo}