def calculate_manhattan_dist(idx, value, n):
    """calculate the manhattan distance of a tile"""
    if idx == value:
        return 0
    # zero is not a tile
    elif value == 0:
        return 0
    else:
        term1 = divmod(value, n)
        term2 = divmod(idx, n)
        return sum(abs(x - y) for x, y in zip(term1, term2))