Everyone knows that Bayview's Computer Club owns a sick gaming PC with very sick RGB lights.
The colours of these RGB lights can be represented by a sequence of length .
is R
if the light is red, G
if it is green, and B
if it is blue.
For some reason, the computer club execs have decided that the current sequence of lights is ugly, and want to change the colours to match the sequence .
Luckily, each of the lights can be toggled with a special remote control. When a light is toggled:
- If the light was red, it becomes green.
- If the light was green, it becomes blue.
- If the light was blue, it becomes red.
Not so luckily, this special remote has malfunctioned, and can only toggle seemingly random contiguous intervals of lights. The computer club execs use the remote times, where during the use, all the lights in the interval are toggled. Please find the earliest time where the sequence of RGB lights equals , or determine if this never occurs.
Constraints
Input Specification
The first line contains a single integer, .
The second line contains letters, the sequence . Each letter is either R
, G
, or B
.
The third line contains letters, the sequence . Each letter is either R
, G
, or B
.
The fourth line contains a single integer, .
The next lines contain two integers each, and .
Output Specification
Output a single integer, the minimal value of such that after applying the first queries, the sequence of RGB lights equals .
If this never occurs, output -1
.
If the sequence of lights equals before any queries, output 0
.
Sample Input 1
3
RRR
BBG
3
1 3
1 2
3 3
Sample Output 1
2
Explanation for Sample Output 1
After the remote is used for the first time, the sequence of lights becomes GGG
.
After the second time, it becomes BBG
, which matches the target sequence.
Sample Input 2
2
RG
BG
2
2 2
1 2
Sample Output 2
-1
Comments