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