I saw multiple times that if an engine, say Carnot engine, absorbs heat $Q_{1}$ at $T_{1}$, does work $W$, and expels heat $Q_{2}$ at $T_{2}$, then we should have$$\tag{1}Q_{1}-Q_{2}=W.$$
Let us say we have a perfect gas within a cylinder+piston going through a Carnot cycle. Initially, the internal energy is $U$, and its temperature $T_1$. $\\$
- Step 1— Isothermal expansion: An amount of heat $Q_1$ is injected into the gas at $T_1$, and an amount of work $W_1$ is done on the surroundings. Furthermore, since we are working with a perfect gas, the internal energy is a function of temperature only. Therefore, since the process is isothermal, the internal energy stays the same, and we have $Q_1 = W_1$. Labeling the internal energy at the end of this process $U_1$, we have $U_1 = U$.
$\\$
- Step 2— Adiabatic expansion: The gas does work $W$ on its surroundings, its temperature drops from $T_1$ to $T_2$. The internal energy at the end of this process is $U_2 = U-W$.
$\\$
- Step 3— Isothermal compression: We do an amount of work $W_2$ on the gas. The gas releases an amount $Q_2$ of heat. The isothermal process and the gas's being perfect imply $Q_2 = W_2$. $U_3 = U_2 = U - W$.
$\\$
- Step 4— Adiabatic compression: We an amount of work $W_3$ on the gas. It's temperature rises to $T_1$. The internal energy after this final process is $U_4 = U_3 + W_3 = U-W+W_3$.
Now, since we want to have a cyclic process, and we want the gas to be restored to its initial state, we must have $U_4 = U$. Thus, $$U-W+W_3 =U,$$ or $$W_3 = W.$$
So the only condition to have a cyclic process is to reinject the same amount of work that was lost during the adiabatic expansion.
How did equation $(1)$ come into being then? It seems to me that $Q_{1}$, $Q_2$, and $W$ are completely independent from each other, aren't they?
It would be very helpful if one could provide an answer without using the concept of entropy, I didn't go through this concept yet.