This really depends on the page types that you're creating. If you have a single page type and only 4 pages then just override the canCreate function (see below) within your Page class. If you have multiple pages but your root level pages are the same page type then use the same method but also add the can_be_root static
var to the child page types (private static $can_be_root = false;
).
e.g.
function canCreate() {
$count = DataList::create(“SiteTree”)->Count();
return ($count > 4) ? false : true;
};
Hopefully this is enough info for your particular situation.
See SilverStripe SiteTree documentation.