I solved the issue, I am getting the correct temperature now.

I changed the code to:

#include <M5StickC.h>
#include <math.h>

const int B = 4275;
const float R0 = 100000;
const int PIN = 33;

void setup() {
M5.begin();

Serial.begin(115200);

}

void loop() {

int a = analogRead(PIN);

float R = 4095.0/a-1.0;
R = R0*R;

float temperature = 1.0 / (log(R / R0) / B + 1 / 298.15) - 273.15;

Serial.println(temperature);

delay(500);
}

The correct pin was 33, and modified the 1023.0 to 4095.0 in the formula because esp32 is returning values between 0 and 4096. But anyway, the temperature readings were too high. Then, I modified the grove cable (as is explained here https://tinkerfarm.net/projects/the-m5stickc/the-5-volt-danger-with-the-m5stickc/) feeding the temperature sensor with 3.3v instead of 5v. And, now, (with no additional code changes) the temperature is correct.