#include <stdio.h>
#include <stdlib.h> // For atoi and exit

int main() {
    int K;
    char input_buffer[1000]; // Buffer to read the entire line of capsule signatures

    // Read K
    if (scanf("%d", &K) != 1) {
        printf("Invalid input\n");
        return 0;
    }

    // Consume the newline character after reading K
    getchar(); 

    // Read the line of capsule signatures
    if (fgets(input_buffer, sizeof(input_buffer), stdin) == NULL) {
        printf("Invalid input\n");
        return 0;
    }

    int capsules[K];
    int count = 0;
    char *token = strtok(input_buffer, " \n"); // Tokenize by space and newline

    while (token != NULL && count < K) {
        int val = atoi(token);
        if (val < 100 || val > 999) { // Check if it's a 3-digit number
            printf("Invalid input\n");
            return 0;
        }
        capsules[count++] = val;
        token = strtok(NULL, " \n");
    }

    if (count != K) { // Check if the number of provided capsules matches K
        printf("Invalid input\n");
        return 0;
    }

    int last_balanced_index = -1;

    for (int i = 0; i < K; i++) {
        int num = capsules[i];
        int first_digit = num / 100;
        int middle_digit = (num / 10) % 10;
        int last_digit = num % 10;

        if ((first_digit + last_digit) == (2 * middle_digit)) {
            last_balanced_index = i;
        }
    }

    if (last_balanced_index != -1 && (last_balanced_index + 1) < K) {
        printf("%d\n", capsules[last_balanced_index + 1]);
    } else {
        printf("-1\n");
    }

    return 0;
}