def minimumBribes(q):
    bribes = 0
    for x in range(len(q)):
        if q[x] > (x + 3):
            print('Too chaotic')
            return
        for j in range(max(0, q[x]-2), x):
            if q[j] > q[x]:
                bribes += 1
    print(bribes)

print(minimumBribes([2, 1, 5, 3, 4]))
print(minimumBribes([2, 5, 1, 3, 4]))

d491b7054e72f922a6e77b67778b5ecfabda9164
<mask token>

def minimumBribes(q):
    bribes = 0
    for x in range(len(q)):
        if q[x] > x + 3:
            print('Too chaotic')
            return
        for j in range(max(0, q[x] - 2), x):
            if q[j] > q[x]:
                bribes += 1
    print(bribes)


<mask token>

def minimumBribes(q):
    bribes = 0
    for x in range(len(q)):
        if q[x] > x + 3:
            print('Too chaotic')
            return
        for j in range(max(0, q[x] - 2), x):
            if q[j] > q[x]:
                bribes += 1
    print(bribes)


print(minimumBribes([2, 1, 5, 3, 4]))
print(minimumBribes([2, 5, 1, 3, 4]))

def minimumBribes(q):
    bribes = 0
    for x in range(len(q)):
        if q[x] > (x + 3):
            print('Too chaotic')
            return
        for j in range(max(0, q[x]-2), x):
            if q[j] > q[x]:
                bribes += 1
    print(bribes)

print(minimumBribes([2, 1, 5, 3, 4]))
print(minimumBribes([2, 5, 1, 3, 4]))
