Skip to main content
Back to problems
Codeforces
Easy
Math
Greedy
Playing with Dice

Choose distinct dice values from 1 to 6 so that Alice's die is strictly larger than Bob's and the score difference is as small as possible.

Acceptance 0%
Also Available On
Other platform versions and source mappings for the same problem.
Problem Statement

Problem

You are given two integers describing the values shown on two dice. You may increase either die's value to any value from 1 to 6, but you cannot decrease it.

Your task is to determine the minimum total number of increments needed to make the first die strictly greater than the second die.

If it is already impossible to make the first die strictly greater than the second die under the dice limits, report that no valid adjustment exists.

Goal

Find the smallest change needed so that:

  • both final values stay in the range [1,6][1, 6],
  • the first value becomes strictly larger than the second value,
  • and the total increase is minimized.

Input Format

  • Two integers aa and bb — the current values on the two dice.
  • Both values are in the range $1 to \6$.

Output Format

  • Print the minimum total number of increments needed to make a>ba > b.
  • If it cannot be done, print -1.

Constraints

  • 1a,b61 \le a, b \le 6
  • Final values must remain between $1 and \6$
  • Only increments are allowed
Examples
Sample cases returned by the problem API.

Example 1

Input

1 1

Output

1

Explanation

Increase the first die to 2. Then 2 > 1, and the total increment is 1.

Example 2

Input

4 2

Output

0

Explanation

The first die is already greater than the second die, so no change is needed.

Show 1 more example

Example 3

Input

2 6

Output

-1

Explanation

The second die is already at the maximum value, and the first die cannot exceed 6 using increments only.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.