def get_n_beads(n, i1 ,i2):
    """
    given length of the ring polymer (n) and the starting-ending positions (i1, i2)
    returns the length of the segment of the smaller length

    :param n: number of elements
    :type n: int
    :param i1: starting position
    :type i1: int
    :param i2: ending position
    :type i2: int
    :return: length of the smaller segment
    :rtype: int
    """
    if (n < max(i1,i2)) | (i1<0) | (i2<0):
        return  0
    else:
        return min(abs(i2-i1)+1, n - abs(i2-i1)+1)