Let G be a graph and let mi,j(G), i, j ≥ 1, be the number of edges uv of G such that {dv(G), du(G)} = {i, j}. The M-polynomial of G is M (G; x, y) = i≤j mi,j(G)x i y j . A general method for calculating the M-polynomials for arbitrary graph families is presented. The method is further developed for the case where the vertices of a graph have degrees 2 and p, where p ≥ 3, and further for such planar graphs. The method is illustrated on families of chemical graphs.