def rescue_walk(algns1, algns2, max_molecule_size):
    """
    Rescue a single ligation that appears as a walk.

    Checks if a molecule with three alignments could be formed via a single 
    ligation between two fragments, where one fragment was so long that it 
    got sequenced on both sides.

    Uses three criteria:
    a) the 3'-end alignment on one side maps to the same chromosome as the 
    alignment fully covering the other side (i.e. the linear alignment)
    b) the two alignments point towards each other on the chromosome
    c) the distance between the outer ends of the two alignments is below
    the specified threshold.


    Alternatively, a single ligation get rescued when the 3' sub-alignment 
    maps to multiple locations or no locations at all.
    
    In the case of a successful rescue, tags the 3' sub-alignment with
    type='X' and the linear alignment on the other side with type='R'.

    Returns
    -------
    linear_side : int
        If the case of a successful rescue, returns the index of the side
        with a linear alignment.

    """
    
    # If both sides have one alignment or none, no need to rescue!
    n_algns1 = len(algns1)
    n_algns2 = len(algns2)

    if (n_algns1 <= 1) and (n_algns2 <= 1):
        return None

    # Can rescue only pairs with one chimeric alignment with two parts.
    if not (
        ((n_algns1 == 1) and (n_algns2 == 2))
        or ((n_algns1 == 2) and (n_algns2 == 1))
    ):
        return None

    first_read_is_chimeric = n_algns1 > 1
    chim5_algn  = algns1[0] if first_read_is_chimeric else algns2[0]
    chim3_algn  = algns1[1] if first_read_is_chimeric else algns2[1]
    linear_algn = algns2[0] if first_read_is_chimeric else algns1[0]
    
    # the linear alignment must be uniquely mapped
    if not(linear_algn['is_mapped'] and linear_algn['is_unique']):
        return None

    can_rescue = True
    # we automatically rescue chimeric alignments with null and non-unique 
    # alignments at the 3' side
    if (chim3_algn['is_mapped'] and chim5_algn['is_unique']):
        # 1) in rescued walks, the 3' alignment of the chimeric alignment must be on
        # the same chromosome as the linear alignment on the opposite side of the
        # molecule
        can_rescue &= (chim3_algn['chrom'] == linear_algn['chrom'])

        # 2) in rescued walks, the 3' supplemental alignment of the chimeric
        # alignment and the linear alignment on the opposite side must point
        # towards each other
        can_rescue &= (chim3_algn['strand'] != linear_algn['strand'])
        if linear_algn['strand'] == '+':
            can_rescue &= (linear_algn['pos5'] < chim3_algn['pos5'])
        else:
            can_rescue &= (linear_algn['pos5'] > chim3_algn['pos5'])

        # 3) in single ligations appearing as walks, we can infer the size of 
        # the molecule and this size must be smaller than the maximal size of 
        # Hi-C molecules after the size selection step of the Hi-C protocol
        if linear_algn['strand'] == '+':
            molecule_size = (
                chim3_algn['pos5']
                - linear_algn['pos5']
                + chim3_algn['dist_to_5']
                + linear_algn['dist_to_5']
            )
        else:
            molecule_size = (
                linear_algn['pos5']
                - chim3_algn['pos5']
                + chim3_algn['dist_to_5']
                + linear_algn['dist_to_5']
            )

        can_rescue &= (molecule_size <= max_molecule_size)
    
    if can_rescue:
        if first_read_is_chimeric:
            algns1[1]['type'] = 'X'
            algns2[0]['type'] = 'R'
            return 2
        else:
            algns1[0]['type'] = 'R'
            algns2[1]['type'] = 'X'
            return 1
    else:
        return None