아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Marbles

시간 제한2초메모리 제한128 MB

요약
선분 위에서 구슬이 튕기며 움직일 때, 모든 스위치가 동시에 구슬로 덮이는 최소 시간을 구하거나 -1을 출력한다.
난이도

보통10점 중 7점

유형
정렬, 수학, 시뮬레이션
정답자
아직 제출이 없습니다

문제

You are given a track of length LL. On it there are NN marbles at positions A_1,A_2⋯ ,A_NA\_1,A\_2\cdots,A\_N  and NN switches at positions B_1,B_2,⋯ ,B_NB\_1,B\_2,\cdots,B\_N, both of negligably small size. In the beginning, you direct each marble left or right and they all start moving at the constant speed of 11 in the given direction. When two marbles collide, they bounce off each other elastically, meaning that they continue moving in opposite directions at the same speed. If a marble collides with the beginning or end of the track it bounces off with the same speed in the opposite direction. Keep in mind that it takes exactly one second for a marble at position ii to travel to the position i+1i+1 or i−1i-1, and it also takes exactly one second to travel from 11 back to 11 while changing directions, or to travel from LL back to LL while changing directions. Note that marbles never stop on the switches.

The goal is to have a marble on top of every switch, i.e. to have all marbles simultaneously on the corresponding switches.

You need to find the minimum amount of time needed to achieve this.

입력

In the first line of input you are given LL (L≤109L\le 10^9) and NN (N≤3000N\le3000)

In the second line of input you are given A_1,A_2⋯ ,A_NA\_1,A\_2\cdots,A\_N, the initial positions of the marbles. All A_iA\_i are guaranteed to be distinct, and 1≤A_i≤L1\le A\_i\le L.

In the second line of input you are given B_1,B_2⋯ ,B_NB\_1,B\_2\cdots,B\_N, the initial positions of the marbles. All B_iB\_i are guaranteed to be distinct, and 1≤B_i≤L1\le B\_i\le L.

출력

Print one integer: the minimum amount of time to have a marble on top of every switch. If solution does not exist, print −1-1.

예제1

  1. 예제 1

    입력
    4 2
    2 4
    1 4
    
    예상 출력
    1