The problem you are having is integer overflow. The int allocates enough memory to hold a number within a range (specifically 32 bits giving a range of 2^-31 through 2^31 which is -2147483648 to 2147483647), when you add two numbers together and the result is larger then this than it overflows the memory available. Due to the way in which numbers are represented and added in binary, the effect it has is that it wraps around so 2147483647 + 1 = -2147483648.
You can look into integer overflows, to fully understand what is happening it is best to look at how numbers are represented in binary, twos complement, and how to do addition/subtraction. However for the problem at hand you have two options.
First you can use a long which allocates twice as much memory for the numbers and will give you a much larger range before you run into the same problem, but you will run into the same problem with big enough numbers. This is the easiest option, you should be able to just change “int” to “long” when declaring the variables and you will be good.
Alternatively you can look at something like the BigInteger class which will be able to allocate the right amount of memory to hold larger numbers as required, with some small costs in performance and being more awkward to use in some situations.