A factory is trying to pack pencils, but if you pack them in 5 units, 4 will remain, if you pack in 7 units, 6 units will remain, and if you pack in 8 units, 7 units will remain. If the number of pencils made by this factory is the smallest natural number between 1000 and 9999, how many pencils are there?
1000 9999 1 [OP_LIST_ARANGE] 8 7 [OP_LIST_DIVIDE_AND_REMAIN] 7 6 [OP_LIST_DIVIDE_AND_REMAIN] 5 -1 [OP_LIST_DIVIDE_AND_REMAIN] 1 [OP_LIST_MIN]
var_a = 1000
var_b = 9999
var_c = 1
list_a = [i for i in range(var_a, var_b + 1, var_c)]
var_d = 8
var_e = 7
list_b = [] 
var_d = int(var_d)
var_e = int(var_e)
if var_e < 0:
    var_e = var_e + var_d
for i in list_a:
    i = int(i)
    if i%var_d == var_e:
        list_b.append(i)
var_f = 7
var_g = 6
list_c = [] 
var_f = int(var_f)
var_g = int(var_g)
if var_g < 0:
    var_g = var_g + var_f
for i in list_b:
    i = int(i)
    if i%var_f == var_g:
        list_c.append(i)
var_h = 5
var_i = -1
list_d = [] 
var_h = int(var_h)
var_i = int(var_i)
if var_i < 0:
    var_i = var_i + var_h
for i in list_c:
    i = int(i)
    if i%var_h == var_i:
        list_d.append(i)
var_j = 1
list_e=list_d.copy()
list_e.sort()
var_k = list_e[var_j-1]
print(int(var_k))