def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    ls = [0] * k
    pos = 0
    for i in range(n):
        ls[pos] += 1
        if pos + 1 < k:
            pos += 1
    print(0 if len(ls) == 1 else ls[-1] - ls[0])


if __name__ == '__main__':
    slove() def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    ls = [0] * k
    pos = 0
    for i in range(n):
        ls[pos] += 1
        if pos + 1 < k:
            pos += 1
    print(0 if len(ls) == 1 else ls[-1] - ls[0]) slove import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline n, k = list(map(int, input().rstrip('\n').split())) n list(map(int, input().rstrip('\n').split())) list map(int, input().rstrip('\n').split()) map int input().rstrip('\n').split() ().rstrip('\n').split ().rstrip('\n') ().rstrip () input rstrip '\n' split k ls = [0] * k ls [0] * k [0] 0 k pos = 0 pos 0 for i in range(n):
        ls[pos] += 1
        if pos + 1 < k:
            pos += 1
     i range(n) range n ls[pos] += 1 ls[pos] ls pos 1 if pos + 1 < k:
            pos += 1
     pos + 1 < k pos + 1 pos 1 k pos += 1 pos 1 print(0 if len(ls) == 1 else ls[-1] - ls[0]) print 0 if len(ls) == 1 else ls[-1] - ls[0] len(ls) == 1 len(ls) len ls 1 0 ls[-1] - ls[0] ls[-1] ls -1 ls[0] ls 0 if __name__ == '__main__':
    slove() __name__ == '__main__' __name__ '__main__' slove() slove def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    ls = [0] * k
    pos = 0
    for i in range(n):
        ls[pos] += 1
        if pos + 1 < k:
            pos += 1
    print(0 if len(ls) == 1 else ls[-1] - ls[0]) def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    ls = [0] * k
    pos = 0
    for i in range(n):
        ls[pos] += 1
        if pos + 1 < k:
            pos += 1
    print(0 if len(ls) == 1 else ls[-1] - ls[0]) slove
def slove():
    import sys
    input = sys.stdin.readline
    n, k = list(map(int, input().rstrip('\n').split()))
    ls = [0] * k
    pos = 0
    for i in range(n):
        ls[pos] += 1
        if pos + 1 < k:
            pos += 1
    print(0 if len(ls) == 1 else ls[-1] - ls[0])


if __name__ == '__main__':
    slove()
