All those 40 million customers are already on a 50 Hz grid, it's just that the phase of the Ukrainian grid might be slightly different from the European grid. You can (with the correct know-how, training and access to generator controls!) fairly easily get one of the two to speed up or slow down by 0.1 Hz or something like that, wait until the grids match phase and then bring the frequencies back together. Now that they are the same frequency and phase, you can connect the networks just by flipping a switch and provided they were both in balance before the procedure no current will flow initially. Afterwards the two separate networks have effectively merged into one bigger network.
It seems very complicated, but bringing generators and subgrids into sync is something that network operators do all day long. (Ship engineers too, since they can't rely on shore power for obvious reasons! You can have some very "interesting" outages if you forget to synchronize a newly started generator before connecting to the rest of the ship electricity system.)