Under construction

Still being built. Things may change

Bursting the Balloons

Interview

Time limit0.25sMemory limit512 MB

Summary
Given N balls and K baskets, decide whether N can be split into K distinct positive integers and, if so, print the minimum possible difference between the largest and smallest part.
Level

Medium5 of 10

Topics
Math, Greedy, Binary search, Implementation
Solved
No attempts yet

Problem

KK teams play a balloon bursting game. Each team has one basket, and must throw the balls in its basket to burst its own balloon.

To prepare the game, we must distribute NN balls into the KK baskets. For the game to be fun, the numbers of balls in the baskets must all be different. That is, the NN balls must be distributed into the KK baskets with none left over, each basket must hold at least 1 ball, and the numbers of balls in the baskets must all be distinct.

To reduce the unfairness of the game, we will distribute the balls so that the difference between the number of balls in the fullest basket and the number in the emptiest basket is minimized.

The rules for distributing the balls into the baskets are as follows.

  1. The NN balls are distributed into the KK baskets with none left over.
  2. Each basket holds at least 1 ball.
  3. The numbers of balls in the baskets are all distinct.
  4. The difference between the number of balls in the fullest basket and the number in the emptiest basket is minimized.

Write a program that, when distributing NN balls into KK baskets in a way that satisfies all the rules above, determines whether such a distribution is possible, and if it is, computes and prints the difference between the number of balls in the fullest basket and the number in the emptiest basket.

Input

The first line gives NN, the number of balls, and the integer KK, the number of teams.

Output

If the NN balls can be distributed into the KK baskets in a way that satisfies the rules, print the difference between the number of balls in the fullest basket and the number in the emptiest basket. If they cannot be distributed, print -1.

Constraints

  • 2≤N≤100,0002 \le N \le 100,000
  • 2≤K≤1,0002 \le K \le 1,000

Examples2

  1. Example 1

    Input
    5 3
    
    Expected output
    -1
    
  2. Example 2

    Input
    6 3
    
    Expected output
    2