First, evaluate the integral:
#color(white)=int((3x+1)^2-2x-1)# #dx#
#=int(3x+1)^2# #dx# #-int2x# #dx# #-int1# #dx#
#=int(3x+1)^2# #dx# #-(2x^2)/2-x+C#
#=int(3x+1)^2# #dx# #-x^2-x+C#
Let #u=3x+1#, which means #du=3dx#, or #dx=(du)/3#:
#=intu^2/3# #du# #-x^2-x+C#
#=1/3intu^2# #du# #-x^2-x+C#
#=1/3*u^3/3-x^2-x+C#
#=u^3/9-x^2-x+C#
#=(3x+1)^3/9-x^2-x+C#
#=(27x^3+27x^2+9x+1)/9-x^2-x+C#
#=3x^3+3x^2+x+1/9-x^2-x+C#
#=3x^3+2x^2+C#
#=x^2(3x+2)+C#
Now, plug in #x=2# and set this equal to #5# and solve for #C#:
#2^2(3(2)+2)+C=5#
#4(8)+C=5#
#32+C=5#
#C=-27#
This is our set constant. This means that our #f(x)# is:
#f(x)=x^2(3x+2)-27#
Hope this helped!