Under construction

Still being built. Things may change

Revolving Sushi

Time limit2sMemory limit1024 MB

Summary
Given N setters with sushi counts and N+1 rotating cooks, find the minute when every setter has eaten all leftover sushi in sets of K, or -1 if never.
Level

Medium6 of 10

Topics
Simulation, Math, Implementation
Solved
No attempts yet

Problem

The UCPC city has the most famous conveyor belt sushi restaurant in the world. Its name is "the restaurant where everything revolves"! The restaurant became popular on social media for its unusual concept: the cooks and the guests all revolve. The UCPC problem setters booked a table months ago, and today they finally visited.

A total of NN problem setters visited the restaurant, so the kitchen prepared N+1N+1 cooks to match. The ii-th cook can make bib_i pieces of sushi per minute. At the start, the waiter seated the problem setters in a row and gave aia_i pieces of sushi to the ii-th setter. Then the following actions repeated in order every minute.

  1. For every ii with 1≤i≤N1\leq i\leq N, the cook at position ii makes sushi for the setter at position ii. The amount made equals the number of pieces that cook can make per minute. The cook at position N+1N+1 makes no sushi and rests.
  2. The cooks rotate once. That is, the cook originally at position 1 moves to position 2, the cook originally at position 2 moves to position 3, ..., and the cook originally at position N+1N+1 moves to position 1.
  3. The setters rotate once. That is, the setter originally at position 1 moves to position 2, the setter originally at position 2 moves to position 3, ..., and the setter originally at position NN moves to position 1.

Each setter eats one set of sushi as soon as they have one set gathered. A set is a bundle of KK pieces, regardless of type. Time spent eating is ignored.

The setters hate wasting food, so they want to keep eating until everyone has 0 pieces left. How many minutes after the meal starts will the meal end?

Input

The first line contains integers NN and KK, separated by a space. (1≤N≤2 0001\leq N\leq 2\,000, 2≤K≤1 000 0002\leq K\leq 1\,000\,000)

The second line contains a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N, separated by spaces. (0≤ai≤K−10\leq a_i\leq K-1)

The third line contains b1,b2,⋯ ,bN+1b_1, b_2, \cdots, b_{N+1}, separated by spaces. (1≤bi≤K−11\leq b_i\leq K-1)

Output

Print the number of minutes it takes the setters to finish the meal. If the setters never finish the meal, even after infinite time, print -1 instead.

Examples2

  1. Example 1

    Input
    3 3
    0 0 1
    2 1 1 2
    
    Expected output
    3
    
  2. Example 2

    Input
    3 3
    0 0 0
    2 1 1 2
    
    Expected output
    0