Fair Photography

No attempts yetTime limit1sMemory limit128 MB

Problem

FJ has NN cows (1N1000001 \le N \le 100\,000) standing along a long one-dimensional fence. Cow ii stands at position xix_i (integer in 010000000000 \ldots 1\,000\,000\,000) and has breed bib_i (181 \ldots 8). No two cows share a position.

FJ wants a photo of a contiguous interval of cows. Every breed that appears in the photo must appear the same number of times (for example, 27 of breed 1 and 27 of breed 3 is fine, but 9 of breed 1 and 10 of breed 3 is not). At least KK breeds (K2K \ge 2) must appear.

Find the maximum photo size, defined as the difference between the largest and smallest positions among cows in the photo. If no valid photo exists, output 1-1.

Input

  • Line 1: NN and KK.
  • Next NN lines: xix_i and bib_i.

Output

One integer: the maximum fair photo size, or 1-1 if none exists.

Hint

Sort by position, then check each contiguous interval for equal per-breed counts.