Section 39. Any person who knowingly violates any provision of sections twenty-one or twenty-two shall be punished by imprisonment for not more than six months or by a fine of not more that fifteen hundred dollars, or by both. Whoever violates any of the provisions of said sections after one or more prior convictions of a violation of any provision of this chapter, or of a provision of prior law relating to narcotic drugs or harmful drugs as defined in said prior law shall be punished by imprisonment for not more than two years or by a fine of not more than two thousand dollars, or both.