Optimization function returns the same optimal parameters for two labels
The error stemmed apparently from the formula for the gradient: there should be no minus sign (-) at the start of it. The correct code for this would be:

function [jValue, gradient] = costFunction(theta, m, X, Y)
    V = X*theta;
    jValue = (-1/m)*sum(Y.*log(sigmoid(V)) + (1-Y).*log(1-sigmoid(V)));
    gradient = (1/m)*X'*(sigmoid(V)-Y);
end