Problem:
Score : 300 points

Problem Statement

You are given two non-negative integers L and R.
We will choose two integers i and j such that L \leq i < j \leq R.
Find the minimum possible value of (i \times j) \mbox{ mod } 2019.

Constraints

All values in input are integers.

0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition.

Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 2.

Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).

Solution in Scala (2.11.7) (judge verdict: Wrong Answer)

import java.util.Scanner

import scala.annotation.tailrec
import scala.collection.mutable.ArrayBuffer


object Main {
  def read() = {
    val sc = new Scanner(System.in)
    val l, r = sc.nextInt()
    (l, r)
  }

  def solve(l: Int, r: Int): Long = {
    val mod = 2019
    // 0~2018が必要条件
    val ml = l % mod
    val mr = r % mod
    // ml <= mr の場合: [ml, mr]の範囲
    // ml > mrの場合: [0, mr], [ml, 2018]

    if(mr < ml) return 0
    var dl = l / mod
    var dr = r / mod

    val range: IndexedSeq[Int] = ml to mr
    if(dl == dr) {
      (for (i <- range; j <- range; if i < j) yield {
        i * j % mod
      }).min
    }else{
      (for (i <- range; j <- range; if i <= j) yield {
        i * j % mod
      }).min
    }
  }

  def main(args: Array[String]): Unit = {
    val (l, r) = read()
    println(solve(l, r))
  }
}