from typing import Sequence


def decide_compression(a: Sequence[int], b: Sequence[int], tie: bool) -> int:
    """Deterministically decide on a compression method according to each users' preferences.

    For each method in common, assign it a "cost" of (a.index()^2 * p) + (b.index()^2 * q) and select the method with
    the smallest "cost". p and q are chosen to be adjacent coprimes. If the ID of a is greater than the ID of b
    (indicated by the tie parameter), then q = p + 1, otherwise p = q + 1.

    Squaring the index is done to encourage the algorithm to select low index methods on *both* preference lists,
    rather than just the first. Otherwise you frequently end up with selections where you get A's favorite and B's
    least favorite method.
    """
    potential = set(a).intersection(b)  # elements in both a and b
    if potential:
        if tie:
            p, q = 6, 7
        else:
            p, q = 7, 6

        def key(x):
            a_factor = a.index(x)
            b_factor = b.index(x)
            return a_factor * a_factor * p + b_factor * b_factor * q
        return min(potential, key=key)
    return 0