'''
https://projecteuler.net/problem=2
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
'''

# limit = 4000000
# fib = [1,2]
# i = len(fib)
# while True:
#     nxt = fib[i-1]+fib[i-2]
#     if nxt > limit:
#         break
#     fib.append(nxt)
#     i +=1
# print(fib)
# summ = 0
# for i in range(len(fib)):
#     if not fib[i] % 2: # páros
#         summ += fib[i]
# print(summ)

limit = 4000000
a = 1
b = 1
sum = 0
while True:
    b, a = a+b, b
    if not b % 2:
        sum += b
    if b > limit:
        break
print(sum)
