Dynamics of systems with varying number of particles: From Liouville equations to general master equations for open systems