def nonoverlap_len(bed):
    """Calculate total non-overlap length of a set of regions.
    
    Args:
        bed (pandas.DataFrame): A sorted dataframe in BED format. It should 
            have at least four columns for "chrom", "chromStart", "chromEnd" 
            and "geneName/ID". This sorted dataframe must have a single 
            "chrom" value for all regions and is expected to be sorted 
            first by "chromStart" and then by "chromEnd" Columns should be in 
            the expect order (i.e. values will be selected by position, 
            ``iloc``), though the name is not important.
    
    Return:
        length (int): the total non-overlap length of all regions in the input 
            BED.
    """
    
    assert len(bed.iloc[:, 0].unique()) == 1
    length = 0
    start = 0
    end = -1
    for _, row in bed.iterrows():
        if row.iloc[1] > end + 1:
            length += end - start + 1
            start = row.iloc[1]
            end = row.iloc[2]
        elif row.iloc[2] > end:
            end = row.iloc[2]
    length += end - start + 1
    return length