Hello, welcome to just answer
Here is what each code means
Code P0174 = Engine started, engine running at cruise speed for 3 to 4 minutes, and the PCM detected the Bank 2 Adaptive Fuel Control System reached its rich correction limit (a lean A/F condition).
Code P0171 = Engine started, engine running at cruise speed for 3 to 4 minutes, and the PCM detected the Bank 1 Adaptive Fuel Control System reached its rich correction limit (a lean A/F condition).
Here is a list of possible causes to set these and cause the symptoms you are having
Carefully inspect the following areas for potential air leaks:
Hoses connecting to MAF
Hoses connecting to throttle body
Intake manifold gasket leaks
Vacuum lines disconnected
Improperly seated dip stick and tube
Exhaust leaks at flanges and gaskets
If any problems are found and you repair them, then clear codes and unhook negative battery cable for approximately 2 minutes. Reconnect negative battery cable and start vehicle to let computer relearn idle.
The reason it is okay for about 20 minutes after hooking up battery is because vehicle is running in open loop and does not care what the oxygen sensors are saying. It delivers a predetermined amount of fuel to engine until operating temperature comes up. Once up to operating temperature it goes to closed loop and now you have problems because it takes inputs from all sensors to make fuel adjustments and with a bad vacuum leak persay it will cause the problem you are having and cause those codes to set.
Good luck, if you need anything in the future you can always request me or speak to another just answer expert.