IDENTIFICATION DIVISION.
PROGRAM-ID. 158A.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP        PIC X(100).
01 N          PIC 9(19).
01 A          PIC 9(18).
01 B          PIC 9(18).
01 SUM-A      PIC 9(19).
01 SUM-B      PIC 9(19).
01 BK-CNT     PIC 9(18).
01 CNT        PIC 9(18). 

01 OUT-CNT    PIC Z(18)9.
 
PROCEDURE DIVISION.
MAIN-001.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ALL ' '
      INTO N A B.
 
  MOVE 0 TO SUM-A SUM-B.
  MOVE 1 TO CNT.
  PERFORM UNTIL (N <= SUM-A + SUM-B)
    COMPUTE SUM-A = SUM-A + A
    COMPUTE SUM-B = SUM-B + B
  END-PERFORM.

  COMPUTE BK-CNT = SUM-A + SUM-B - N.
  IF (BK-CNT <= B)
    MOVE SUM-A TO OUT-CNT 
  ELSE
    COMPUTE OUT-CNT = SUM-A - (BK-CNT - B)
  END-IF.

  DISPLAY OUT-CNT.
 
MAIN-EXIT.
  STOP RUN.
