Problem:
Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

use std::io::{stdin, Stdin, BufReader, Read, BufRead};

fn main() {
    let stdin = stdin();
    let A = read(&stdin);
    let B = read(&stdin);
    let C = read(&stdin);
    let X = read(&stdin);
    assert_eq!(X % 50, 0);

    let mut pairs: Vec<(i32, i32, i32)> = Vec::new();

    // 500円硬貨から
    for a in 0..A+1 {
        let mut sum = 0;
        if a * 500 > X {
            continue;
        }
        sum += a * 500;

        for b in 0..B+1 {
            if sum + b * 100 > X{
                continue;
            }
            sum += b * 100;

            for c in 0..C+1 {
                let cur_sum = sum + c * 50;
                if cur_sum == X {
                    pairs.push((a, b, c));
                }
            }
            sum = 0;
        }
    }

    println!("{}", pairs.len());
}

fn read(stdin: &Stdin) -> i32 {
    let mut buf = String::new();
    stdin.lock().read_line(&mut buf).unwrap();
    buf.pop();
    buf.parse().unwrap()
}