Description
Solutions
Find Last Affected System

A company Amania has to store a critical key in a secure format. In order to ensure that the key is not leaked easily, they have decided to break the key into pieces and store it on 'N' systems that are connected in the form of a ring. All the systems of the network have a unique ID numbered from 0 to N-1. A notorious hacker has affected the system with a virus to find the key. The virus starts affecting the network from the Kth system. It proceeds in a fixed clockwise direction in the ring. In each step, it skips K-1 number of nodes and affect the Kth system. The affected system is then eliminated from the network.

The software engineers at the company are able to identify the pattern in which the virus is affecting the systems. Since, the company does not want the hackers to find the complete key so they want to remove one of the systems from the network. The company is not aware of the number of the systems that have been affected and they decide to remove the last system affected by the virus.

Write an algorithm that will help identify the system that should be removed from the network.

Input

The first line of the input consists of two space-separated integers - numSystem and count, representing the number of systems that are connected in the network (N) and the value K respectively.

Output

Print an integer representing the ID of the last system that will be affected last by the virus.

Note

The time that a virus takes to affect the file is ignored

Example 1:

Input:  numSystem = 3, count = 2
Output: 1
Explanation:
Unfortunately, bro MI A3 AI TRIPLE CAMERA didn't catch the explanation for this question 🥹
Constraints:
  • 1 ≤ numSystem ≤ 10^8
  • 1 ≤ count ≤ 10^8
Thumbnail 0
Testcase

Result
Case 1

input:

output: