P0140 could be bad O2 sensor bank 1 sensor 2 which is the sensor after the catalytic converter.
A dirty MAFS (mass air flow sensor) could cause the the issue so try cleaning it first before replacing the O2 sensor.
P0171 could be many things, the most likely cause being a dirty or faulty MAFS sensor
Try removing and cleaning the MAFS sensor first, its easy to do - if you do not know how you can google it or look on youtube for a tutorial