def f(aa, a, b)
i = aa.index(a)
sa = aa[i..-1] + aa[0, i]
sa -= (sa - b)
i = b.index(sa[0])
b[i..-1] + b[0,i]
end
p f([2,3,5,7,9], 2, [9,2,5])
p f([2,3,5,7,9], 9, [9,2,5])
p f([2,3,5,7,9], 5, [9,2,5])
p f([2,3,5,7,9], 3, [9,2,5,5,2])
ZGVmIGYoYWEsIGEsIGIpCglpID0gYWEuaW5kZXgoYSkKCXNhID0gYWFbaS4uLTFdICsgYWFbMCwgaV0KCXNhIC09IChzYSAtIGIpCglpID0gYi5pbmRleChzYVswXSkKCWJbaS4uLTFdICsgYlswLGldCmVuZAoKcCBmKFsyLDMsNSw3LDldLCAyLCBbOSwyLDVdKQpwIGYoWzIsMyw1LDcsOV0sIDksIFs5LDIsNV0pCnAgZihbMiwzLDUsNyw5XSwgNSwgWzksMiw1XSkKcCBmKFsyLDMsNSw3LDldLCAzLCBbOSwyLDUsNSwyXSkK