How to write custom stencil optionally?

Here is the situation:
A mesh has written the stencil with 2. And another mesh writes the stencil in the zone with stencil not equal 2 and depth greater than the mesh.