Under construction

Still being built. Things may change

Jollo

Interview

Time limit1sMemory limit128 MB

Summary
Given the Princess's three cards and two of the Prince's, find the smallest unused third card that lets the Prince win at least two rounds against any order of play.
Level

Medium5 of 10

Topics
Brute force, Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

Jollo is a simple card game that children love to play. Two players compete with a standard deck of 52 cards. The 52 cards are ordered by rank and suit into a single sequence of distinct values, so any two cards can be compared and a tie can never occur.

A game is a best-of-three series: the first player to win two rounds wins the game. At the start the deck is shuffled and each player is dealt a hand of three cards. In each round both players reveal one card at the same time, and the player who shows the higher card wins that round. Revealed cards are discarded and can never be shown again.

The King's son loves this game, but he is not very clever and often loses to his little sister. Whenever he loses he cries so loudly that no one can stand it. The servant who deals the cards is afraid he will be punished if the Prince keeps losing. The servant may look at every card he deals. After dealing five cards (three to the Princess and two to the Prince), he wants to know the lowest-valued card he could deal to the Prince as a third card so that the Prince can never lose the game, no matter how poorly he plays.

In other words, find the lowest not-yet-dealt third card ZZ for the Prince such that, however the Prince's three cards {X,Y,Z}\{X, Y, Z\} are matched against the Princess's three cards {A,B,C}\{A, B, C\} over the rounds, the Prince wins at least two of the three rounds.

Input

Each test case is a single line containing five distinct integers AA, BB, CC, XX, YY. The first three, AA, BB, CC (1≤A,B,C≤521 \le A, B, C \le 52), are the Princess's cards, and the last two, XX, YY (1≤X,Y≤521 \le X, Y \le 52), are the Prince's cards. The five integers on a line are all distinct.

The input contains several test cases. The last one is followed by a line of five zeros, which must not be processed.

Output

For each test case print a single line. If, among the 47 cards not yet dealt, there is a card that guarantees the Prince cannot lose the game no matter how the rounds are played when given as his third card, print the lowest-valued such card. Otherwise print -1.

Examples4

  1. Example 1

    Input
    28 51 29 50 52
    50 26 19 10 27
    10 20 30 24 26
    46 48 49 47 50
    0 0 0 0 0
    
    Expected output
    30
    -1
    21
    51
    
  2. Example 2

    Input
    1 2 3 51 52
    52 40 30 41 31
    13 27 41 26 40
    0 0 0 0 0
    
    Expected output
    4
    -1
    -1
    
  3. Example 3

    Input
    50 51 52 1 2
    0 0 0 0 0
    
    Expected output
    -1
    
  4. Example 4

    Input
    48 49 50 51 1
    0 0 0 0 0
    
    Expected output
    52