Minimal invariant varieties and first integrals for algebraic foliations