def epoch_difference(M, b_prd, e_prd):
    """
    Compute difference between two epochs accounting for end of horizon circling

    :param M: Model
    :param b_prd: global start period
    :param e_prd: global end period
    :return: end - start + 1 (+ n_prds_per_cycle if spans end of horizon)
    """

    if e_prd >= b_prd:
        return e_prd - b_prd + 1
    else:
        return M.n_prds_per_cycle + e_prd - b_prd + 1