#!/usr/bin/env perl
use strict;
use warnings;

open (my $fh, '<', 'input') or die "$!";

my @items;
{
    local $/;
    my $content = <$fh>;
    @items = split("\n", $content);
}

# HACK: don't have to compute ID for all of them--sort then compute ID
#
# BUT: we'll brute force it...
my @ids = map { id($_) } @items;
@ids = sort { $b <=> $a } @ids;

# NOTE: not first or last
for (my $i = 1; $i < $#ids; $i++ ) {
    if ($ids[$i-1] - $ids[$i] > 1) {
        print $ids[$i] + 1 . "\n"; # 2 seat gap, print middle
        exit(0);
    }
}
exit(1);


sub id {
    my ($v) = @_;
    my $id;
    # row
    for (my $i = 0; $i < 7; $i++) {
        if (substr($v, $i, 1) eq 'B') {
            $id += (2 ** (6 - $i)) * 8;
        } else {
            # F -- lower bound stays same, no addition
        }
    }
    # col
    for (my $i = 7; $i < 10; $i++) {
        if (substr($v, $i, 1) eq 'R') {
            $id += 2 ** (9 - $i);
        } else {
            # R
        }
    }
    return $id;
}
