How about biasing your first set number by -31 so you stay on the legal range:

integer = (firstset - 31) * 1000 + secondset

firstset = (integer \ 1000) + 31

secondset = integer - (firstset - 31) * 1000

mod may well work for the second calculation but I don't recall how Fortran's mod works with negative numbers.

Hope this helps,

-bcl