There are n closets of the same size arranged in a row. There are only n - 2 closet doors, so exactly two closets are always open.
A door in front of a closet can move one position only when the neighboring closet in that direction is open. After the move, the closet the door left becomes open, and the closet the door moved to becomes closed.
You are given the closet numbers that must be used, in order. Move the doors so that each requested closet can be used in that order, and find the minimum possible total number of door moves. Initially, exactly two closets are open.
The first line contains the number of closets, n. n is greater than 3 and at most 20.
The second line contains the numbers of the two closets that are initially open.
The third line contains the number of closets to use, m. m is at most 20.
Each of the next m lines contains one closet number, in the order it must be used.
Output the minimum possible total number of door moves.