the *first Zagreb index*, $M_1(G)$, and the *second Zagreb index*, $M_2(G)$, are defined as $$\begin{aligned}
M_1(G) & = & \sum_{v \in V(G)} \deg(v)^2, \\
M_2(G) & = & \sum_{e=uv \in E(G)} \deg(u) \cdot \deg(v).\end{aligned}$$ If $G$ has no edges, we set $M_1(G)=M_2(G)=0$. Since the introduction, many mathematical and chemical properties were found for these topological indices. For example, it is easy to check that $$\label{zag1}
M_1(G) = \sum_{e=uv \in E(G)} (\deg(u) + \deg(v)).$$

In [@beh] the Wiener polarity index was expressed by using the Zagreb indices and the following theorem was proved.

[@beh] \[behas\] Suppose $G$ is a connected triangle- and quadrangle-free graph such that its different cycles have at most one common edge. If $N_p = N_p(G)$ and $N_h = N_h(G)$ denote the number of pentagons and hexagons of $G$, then $W_p(G) = M_2(G) - M_1(G) -
5N_p - 3N_h + |E(G)|$.

However, many chemical graphs contain $4$-cycles and consequently, Theorem \[behas\] can not be used in such cases. Therefore, in Theorem \[glavni\] we generalize this result such that it can be used also for graphs that contain $4$-cycles. Moreover, in our generalization the number of common edges of different cycles is limited only for small cycles.

To show 