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::*;
use std::str::FromStr;

pub fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn dfs(
    cn: usize,
    g: &Vec<Vec<(usize, usize)>>,
    pn: usize,
    depth: usize,
    dist: &mut Vec<usize>,
) -> bool {
    if dist[cn] != 0 && dist[cn] != depth {
        return true;
    }

    let mut flag = false;
    dist[cn] = depth;

    for i in 0..g[cn].len() {
        let gi = g[cn][i];
        if gi.0 == pn {
            continue;
        }

        let result = dfs(gi.0, g, cn, depth + gi.1, dist);
        if result {
            flag = true;
            break;
        }
    }
    flag
}

struct UnionFind {
    parent: Vec<usize>,
    size: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        // initialize
        let mut v: Vec<usize> = vec![];
        for i in 0..n + 1 {
            v.push(i)
        }
        let mut s: Vec<usize> = vec![1; n + 1];

        UnionFind { parent: v, size: s }
    }

    fn root(&self, x: usize) -> usize {
        if self.parent[x] == x {
            return x;
        } else {
            return self.root(self.parent[x]);
        }
    }

    fn size(&self, a: usize) -> usize {
        let ra = self.root(a);
        self.size[ra]
    }

    fn unite(&mut self, a: usize, b: usize) {
        let mut ra = self.root(a);
        let mut rb = self.root(b);
        if ra == rb {
            return;
        }
        if self.size[ra] < self.size[rb] {
            std::mem::swap(&mut ra, &mut rb)
        };
        self.size[ra] += self.size[rb];
        self.parent[rb] = ra;
    }

    fn same(&self, a: usize, b: usize) -> bool {
        let ra = self.root(a);
        let rb = self.root(b);
        self.parent[a] == self.parent[b]
    }
}

fn main() {
    let n: usize = read();
    let m: usize = read();
    let mut g: Vec<Vec<(usize, usize)>> = vec![vec![]; n + 1];
    let mut uf = UnionFind::new(n + 1);
    let mut done = vec![false; n + 1];

    for _ in 0..m {
        let l: usize = read();
        let r: usize = read();
        let d: usize = read();
        uf.unite(l, r);
        g[l].push((r, d))
    }

    for i in 1..n + 1 {
        if !done[uf.root(i)] {
            let mut dist = vec![0; n + 1];
            if dfs(i, &g, 0, 0, &mut dist) {
                println!("No");
                return;
            }
            done[uf.root(i)] = true;
        }
    }
    println!("Yes");
}