Working on TOJ - Problem 1785