Phase transition from egalitarian to hierarchical societies driven by competition between cognitive and social constraints