function Nout = massConservation(m)

Nin  = null(m.S', 'r');

Nout = Nin;
L = size(Nin,2);

for i = 1:L
    while any(Nout(:,i) < 0)
        jV = [1:i-1 i+1:L];
        for j = 1:length(jV)
            candidates_p2(:,j) = Nout(:,i) + 2*Nout(:,jV(j));
            candidates_p1(:,j) = Nout(:,i) + 1*Nout(:,jV(j));
            candidates_m1(:,j) = Nout(:,i) - 1*Nout(:,jV(j));
            candidates_m2(:,j) = Nout(:,i) - 2*Nout(:,jV(j));
        end
        
        candidates = [candidates_p2 candidates_p1 candidates_m1 candidates_m2];
        
        neg   = sum(candidates < 0);
        pos   = sum(candidates > 0);
        
        if min(neg) == sum(Nout(:,i) < 0)
            Nout(:,i) = -Nout(:,i);
        else
            jBest = find(neg == min(neg));
            jBest = jBest(find(pos(jBest) == min(pos(jBest))));
            jBest = jBest(1);
            Nout(:,i) = candidates(:,jBest);
        end
    end
end