C. Increasing by Modulo
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell (0,0) . Robot can perform the following four kinds of operations:
U — move from (x,y) to (x,y+1) ; D — move from (x,y) to (x,y−1) ; L — move from (x,y) to (x−1,y) ; R — move from (x,y) to (x+1,y) . Vasya also has got a sequence of n operations. Vasya wants to modify this sequence so after performing it the robot will end up in (x,y) .
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: maxID−minID+1 , where maxID is the maximum index of a changed operation, and minID is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices 2 , 5 and 7 are changed, so the length of changed subsegment is 7−2+1=6 . Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is 1 .
If there are no changes, then the length of changed subsegment is 0 . Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from (0,0) to (x,y) , or tell him that it's impossible.